What are Types vs Interfaces in TypeScript?
Learn the real differences between type aliases and interfaces in TypeScript, including declaration merging, unions, and compile-time erasure, with examples.
Expected Interview Answer
A `type` alias and an `interface` both describe the shape of an object, and for plain object shapes they are almost interchangeable, but `interface` supports declaration merging and is limited to object-like shapes, while `type` can alias unions, tuples, primitives, and mapped types.
Interfaces are open — you can declare the same interface name twice and TypeScript merges the members, which is how libraries let consumers augment global types. Type aliases are closed once defined, but they can represent things interfaces cannot, such as `type Id = string | number` or conditional and mapped types. Both support `extends`-like composition: interfaces via `extends`, types via intersection (`&`). At compile time both erase completely, so the choice never affects runtime behavior.
- Interfaces support declaration merging for extensible public APIs
- Types handle unions, tuples, and mapped/conditional types interfaces cannot
- Both compile away to nothing, so no runtime cost either way
- Interfaces read cleanly in class implementation contracts
- Consistent team convention reduces bikeshedding on every PR
AI Mentor Explanation
An interface is like a team's official playing squad list — the board can amend it season to season, adding names under the same registration. A type alias is like a fixed scorecard printed for one match: once the toss happens and the card is set, you don't add players to it, you'd need a fresh card entirely for a different game.
Step-by-Step Explanation
Step 1
Object shapes
Both `interface Foo { a: string }` and `type Foo = { a: string }` describe an object with the same structural contract.
Step 2
Declaration merging
Declaring `interface Foo` twice merges the members into one interface; doing the same with `type Foo` is a compile error (duplicate identifier).
Step 3
Non-object shapes
Unions, tuples, and primitive aliases like `type Status = 'idle' | 'loading' | 'error'` can only be expressed with `type`, not `interface`.
Step 4
Composition syntax
Interfaces compose via `extends`; type aliases compose via intersection (`&`), and can also mix in mapped or conditional types.
Step 5
Erasure at compile time
Both are purely compile-time constructs — the TypeScript compiler strips them entirely, so neither exists in the emitted JavaScript.
What Interviewer Expects
- Explains that both compile away and add zero runtime cost
- Knows interfaces support declaration merging, types do not
- Can name something only `type` can express (unions, tuples, mapped types)
- Mentions `extends` vs `&` as the composition mechanisms
- Avoids claiming one is strictly 'better' without context
Common Mistakes
- Claiming interfaces are faster or produce different JS output
- Believing type aliases can be declaration-merged like interfaces
- Thinking interfaces can alias a union or tuple directly
- Saying one keyword is deprecated in favor of the other
Best Answer (HR Friendly)
“Both `type` and `interface` are ways to describe the shape of data in TypeScript, and for most everyday cases they work interchangeably. The main practical difference is that interfaces can be reopened and extended later, which is handy for shared libraries, while types are more flexible for describing things like a fixed list of allowed values.”
Code Example
interface User {
id: number;
}
interface User {
name: string; // merges automatically
}
const u: User = { id: 1, name: "Ada" }; // valid, merged shape
type Point = { x: number };
type Point = { y: number };
// Error: Duplicate identifier 'Point'.type Status = 'idle' | 'loading' | 'error';
// interface Status = 'idle' | 'loading' | 'error'; // Error: interfaces can't alias unionsFollow-up Questions
- When would you deliberately choose an interface over a type alias?
- How does declaration merging get used in library type definitions?
- Can a class implement a type alias the same way it implements an interface?
- How do intersection types differ from interface extension in edge cases?
- What happens if two merged interfaces declare a property with conflicting types?
MCQ Practice
1. Which feature is unique to `interface` and not available on `type`?
Only interfaces merge automatically when declared more than once with the same name; type aliases throw a duplicate identifier error.
2. Which of these can ONLY be written with `type`, not `interface`?
A union alias like `string | number` has no interface equivalent since interfaces can only describe object shapes.
3. What happens to `interface` and `type` declarations at runtime?
Both are purely compile-time constructs erased during transpilation; nothing about them exists in emitted JavaScript.
Flash Cards
Can interfaces be declared twice with the same name? — Yes — TypeScript merges the members via declaration merging; the same is a compile error for `type`.
Can `type` alias a union like `'a' | 'b'`? — Yes, and `interface` cannot — interfaces only describe object shapes.
How do interfaces compose vs type aliases? — Interfaces use `extends`; type aliases use intersection (`&`).
Do interfaces or types affect emitted JavaScript? — Neither — both are erased completely at compile time.