What is the difference between interface and type alias in TypeScript?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for TypeScript development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
Both interface and type can describe object shapes, but they have key differences. Interfaces: can only describe object shapes; support declaration merging (two interfaces with the same name are merged); are extended with extends; are generally preferred for defining object/class contracts. Type aliases (type): can describe any type — primitives, unions, intersections, tuples, functions; do NOT support declaration merging; use & for intersection (composition); required for union and tuple types. Examples only possible with type: type StringOrNumber = string | number;, type Point = [number, number];, type Callback = () => void;. When to choose: use interface for object shapes in public APIs (because of declaration merging and cleaner error messages); use type for unions, intersections, mapped types, and when describing non-object types. In practice, teams often pick one and stick with it — both are valid.
Common Mistake
Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real TypeScript project.