Types vs Interfaces in TypeScript
Understand when to use type aliases vs interfaces in TypeScript: extends, declaration merging, unions, tuples, and practical conventions for cleaner code.
Expected Interview Answer
Both type aliases and interfaces describe the shape of an object, but interfaces are extendable and support declaration merging, while type aliases are more flexible and can represent unions, intersections, tuples, and primitives that interfaces cannot.
Use an interface when modeling object shapes that may be extended or that public APIs and classes implement, since interfaces support extends and reopening via declaration merging. Reach for a type alias when you need union types, mapped or conditional types, tuples, or aliases of primitives — capabilities interfaces do not have. For a plain object shape the two are largely interchangeable, so many teams standardize on interfaces for objects and reserve type for unions and advanced compositions.
- Interfaces support extends and implements cleanly
- Interfaces allow declaration merging for augmentation
- Type aliases can express unions and intersections
- Type aliases support tuples, mapped and conditional types
- Clear guidance keeps a codebase consistent
AI Mentor Explanation
An interface is like a team's official role sheet you can extend season after season by adding new positions; a type alias is like a fixed match-day squad list that can also say 'either this XI or that XI'. One grows by amendment, the other captures exact alternatives that the role sheet cannot express in a single line.
Step-by-Step Explanation
Step 1
Define an object shape
Both interface User and type User = { ... } describe the same object structure.
Step 2
Extend interfaces
Use extends to build interface hierarchies; classes can implement them.
Step 3
Compose with type aliases
Use type for unions, intersections, tuples, and mapped or conditional types.
Step 4
Use declaration merging
Reopen an interface by declaring it again to augment third-party types; type aliases cannot merge.
Step 5
Pick a convention
Prefer interfaces for object shapes and public APIs, type aliases for unions and advanced compositions.
What Interviewer Expects
- Knows both can describe object shapes
- Explains interfaces support extends and declaration merging
- Explains type aliases handle unions, tuples, and mapped types
- Understands when each is the better choice
- Mentions declaration merging as an interface-only feature
Common Mistakes
- Claiming type aliases cannot describe object shapes
- Thinking interfaces can express union types
- Believing type aliases support declaration merging
- Saying one is strictly better in all cases
Best Answer (HR Friendly)
“Both interfaces and type aliases let you describe the shape of your data in TypeScript. Interfaces are best when the shape may grow or be extended, while type aliases are better when you need to say a value can be one of several options.”
Code Example
// Interface: extendable and mergeable
interface Animal {
name: string;
}
interface Dog extends Animal {
breed: string;
}
// Type alias: unions and intersections
type Status = 'active' | 'inactive' | 'pending';
type Point = { x: number } & { y: number };
const s: Status = 'active';
const d: Dog = { name: 'Rex', breed: 'Lab' };Follow-up Questions
- What is declaration merging and why is it interface-only?
- When would you prefer a type alias over an interface?
- Can a class implement a type alias?
- How do intersection types compare to interface extends?
- What are mapped types and can interfaces express them?
MCQ Practice
1. Which capability belongs to type aliases but NOT interfaces?
Only type aliases can represent union types; interfaces cannot express a union in a single declaration.
2. Which feature is unique to interfaces?
Interfaces support declaration merging (reopening to augment), which type aliases do not.
3. For a plain object shape with no unions or merging, interfaces and type aliases are:
For a simple object shape the two are largely interchangeable; the differences appear with unions, merging, and advanced compositions.
Flash Cards
What can type aliases do that interfaces cannot? — Express unions, intersections, tuples, primitives, and mapped or conditional types.
What can interfaces do that type aliases cannot? — Declaration merging — reopening the same interface to augment it.
Which is preferred for object shapes and public APIs? — Interfaces, because they extend and merge cleanly and read well for OO-style code.
Are they interchangeable for a simple object? — Largely yes; the differences only matter with unions, merging, and advanced type composition.