Skip to content
tezvyn:
Menu

Key differences: type alias vs interface for object shapes

Source: typescriptlang.orgMediumHow cards are made

Tests your grasp of TypeScript extensibility and declaration merging. Contrast interfaces' extends and open merging against type aliases' unions, intersections, and primitives; prefer interfaces for public APIs and types for unions or mapped types.

What's really being asked

This question probes whether you understand TypeScript's type system beyond surface syntax. Interviewers want to see that you know when types are open for extension versus closed, how declaration merging affects library design, and whether you can pick the right tool for modeling objects versus unions or computed types. It separates developers who copy syntax from those who understand compiler behavior and maintainability.

The full answer

A strong answer hits four things in order. First, interfaces support declaration merging, meaning multiple declarations of the same name in the same scope automatically combine, which is critical for augmenting third-party types or evolving public APIs safely. Second, interfaces extend other interfaces using the extends keyword, while type aliases compose via intersection operators, and interfaces generally produce clearer error messages and faster structural comparisons for object shapes. Third, type aliases can represent unions, tuples, mapped types, conditional types, and primitives, whereas interfaces are limited to object-like structures. Fourth, practical preference: use interfaces for public contracts, class implements, and library definitions that consumers might need to augment; use type aliases for unions, derived types, or when you need to alias a complex expression that includes primitives or conditionals.

The mistakes people make

The biggest red flag is claiming the two are identical except for the keyword, because declaration merging and extensibility rules create real differences in large codebases. Another mistake is saying interfaces are always better for performance without explaining why, or asserting that type aliases cannot describe objects at all. Avoid recommending one universally; senior engineers should justify the choice based on team conventions, API surface area, and whether the shape might need augmentation.

What usually comes next

Interviewers often push deeper by asking how you would augment a third-party library type safely, which leads to interface merging. They might ask how to model a response that could be one of several shapes, pushing you toward a type alias with a union. Another follow-up is performance: ask when excessive intersection types on aliases might slow compilation compared to interfaces. They may also ask about class implements and whether there is any difference when a class implements an interface versus a type alias.

A concrete example

Imagine you are building a design system. You define a ButtonProps interface so consumers can merge in their own theme-specific props via declaration merging in a global file. Internally, you derive a StrictButtonProps type using Pick and Omit to create internal variants, because you need a computed mapping, not an extensible contract. The interface stays public and open; the type alias stays internal and closed.

Interview question

When publishing a TypeScript library, why might you export an interface instead of a type alias for a config object?

  • a.Interfaces allow consumers to safely augment the shape via declaration merging.Correct
  • b.Type aliases compile significantly slower when describing large object shapes.
  • c.Interfaces support union types and mapped conditional types for more expressive config.
  • d.Type aliases cannot describe objects that contain methods or nested properties.
Why?

Interfaces support declaration merging, allowing consumers to safely augment public library types, whereas type aliases are closed. Option D is wrong because type aliases can absolutely describe objects with methods and properties; they are not limited to primitives or unions.

Just read this? Test yourself on what you have been reading.

Read the original → typescriptlang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles