Lesson 104 of 296
interface vs type
What you will learn
- Two ways to name a type
- What each can do
- Extending types
- Declaration merging
- Which one to use
On this page
Two ways to name an object type
interface User {
id: number;
name: string;
}
type User = {
id: number;
name: string;
};For simple object shapes, both work the same.
What only type aliases can do
A type can name any type, not only objects:
type Id = string | number; union
type Status = "pending" | "paid"; literal union
type Point = [number, number]; tuple
type Formatter = (value: number) => string; function
type ReadonlyUser = Readonly<User>; utility and mapped typesAn interface can only describe the shape of an object (including functions and classes).
Extending
interface Person {
name: string;
}
interface Employee extends Person {
employeeId: number;
}type Person = { name: string };
type Employee = Person & { employeeId: number };Both give Employee a name and an employeeId. An interface can also extend a type alias of an object, and a type alias can intersect an interface.
Small difference: if extends creates a conflict, the interface shows a clear error. With &, a conflicting property silently becomes never.
Declaration merging (interfaces only)
Two interfaces with the same name in the same scope merge automatically:
interface Window {
myAppVersion: string;
}This adds a property to the built-in Window type. It is how you extend library types (for example, adding a user property to Express's Request type). Two type aliases with the same name are simply an error.
Classes
A class can implements either:
class Admin implements Employee {
name = "Sam";
employeeId = 1;
}Which should you use?
There is no single official rule. A widely used approach:
interfacefor object shapes that describe public APIs and data models, especially if they may be extended (and for declaration merging).typefor unions, tuples, functions, mapped and conditional types, and anything that is not a plain object.- Be consistent within a project.
Many teams simply use type for everything except when they need declaration merging. What matters in an interview is that you know the differences.
Common mistakes
- Trying to write a union with
interface(impossible). - Accidentally merging interfaces by reusing a name.
- Arguing that one is always better.
How to say it in an interview
"For object shapes they are nearly interchangeable. type aliases can name any type, including unions, tuples, functions and mapped or conditional types. Interfaces only describe object shapes, but they support declaration merging, which is how you augment library types like Express's Request, and extends gives clearer errors than intersections. I use interfaces for extendable object contracts and type aliases for everything else, and stay consistent with the codebase."
Key words
| Word | Meaning |
|---|---|
| interface | Names an object shape; can be extended and merged |
| type alias | Names any type |
| extends | Builds an interface from another |
| Declaration merging | Same-name interfaces combining into one |
| implements | A class promising to match a type |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.