Interfaces and Type Aliases
Pick between
interfaceandtypeon the two grounds that actually differ, not on preference.
In this chapter: declaration merging · unions and primitives · extending vs intersecting · optional vs undefined
💡 The Core Idea
interface and type overlap almost completely for object shapes. Two things genuinely differ: only interface merges when declared twice, and only type can name something that is not an object, such as a union, a primitive, a tuple or a function signature. Every other difference people cite is either style or a detail from an old version. Decide on those two, and stop arguing.
How It Works
interface User {
id: number;
name: string;
role: 'admin' | 'user';
avatar?: string; // may be absent entirely
readonly createdAt: Date; // assignable at creation, never after
}readonly and ? are the two modifiers that carry real weight. readonly stops later assignment, and ? makes the key optional. Both are compile-time only.
What only type can do
type ID = string | number; // a union
type Status = 'pending' | 'active'; // literal union
type Pagination = [page: number, limit: number]; // a tuple
type Middleware = (req: Request, next: () => void) => void; // a call signature
type Admin = User & { permissions: string[] }; // an intersectionWhat only interface can do
// Two declarations of one name merge. This is how you add a field to a
// third-party type you do not own — Express's Request, for example
interface Request {
user?: User;
}
interface Request {
requestId: string;
}
// Request now has bothDeclaration merging is a feature at a library boundary and a hazard inside your own code. There, two files that silently add to one type are hard to debug. A duplicate type is an error, which in your own code is usually what you want.
| Capability | interface | type |
|---|---|---|
| Object shape | ✅ | ✅ |
| Union, primitive, tuple | ❌ | ✅ |
| Mapped and conditional types | ❌ | ✅ |
| Declaration merging | ✅ | ❌ |
extends | ✅ | Via & |
implements on a class | ✅ | ✅ |
Extending versus intersecting
interface Product extends BaseEntity and type Product = BaseEntity & { … } reach the same shape by different routes. extends checks compatibility as it goes, so a conflicting property is an error at the declaration. & does not. A conflict silently produces a member of type never, which then fails at the assignment instead. That makes extends the better error message, and it is the reason to prefer it for object shapes.
Discriminated unions
This is the most valuable pattern in the language. It is a union whose members share a field with a literal type, and the compiler uses that field to narrow.
type RequestState<T> =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'success'; data: T }
| { status: 'error'; error: string };
function render(state: RequestState<User>): string {
if (state.status === 'success') return state.data.name; // `data` exists only here
if (state.status === 'error') return state.error; // `error` exists only here
return 'Loading…';
}This is strictly better than an object with optional fields. { data?: T; error?: string } allows both, or neither, and the compiler cannot tell you which state you are in.
Optional versus | undefined
interface A {
age?: number; // the key may be missing
}
interface B {
age: number | undefined; // the key must be present; the value may be undefined
}
const a: A = {}; // ✅
const b: B = {}; // ❌ Property 'age' is missing
const b2: B = { age: undefined }; // ✅Use ? for genuinely optional data. Use | undefined when the caller must acknowledge the field, as in a patch object where "not sent" and "sent as empty" mean different things. exactOptionalPropertyTypes goes further: it stops ? fields from accepting an explicit undefined.
When to Use It
| Scenario | Reach for | Why |
|---|---|---|
| An object shape, especially one others extend | interface | Better errors on conflict; merges for library augmentation |
| A union, tuple, primitive alias or function type | type | interface cannot express them |
| Mutually exclusive states | A discriminated union of types | The compiler proves which fields exist |
| Adding a field to a third-party type | interface declaration merging | The only mechanism that works |
| A derived shape — omit, pick, partial | type with utility types | Stays in step with the source type |
Deriving inputs from the model rather than restating them:
type CreateUserInput = Omit<User, 'id' | 'createdAt'> & { password: string };
type UpdateUserInput = Partial<CreateUserInput>;Common Mistakes
❌ Modelling state with optional fields. { loading?: boolean; data?: T; error?: string } permits eight combinations, most of them meaningless. A discriminated union permits exactly the states that exist.
❌ Using an index signature to avoid naming keys. { [key: string]: string } gives up every autocomplete and typo check on that object. Record<'submit' | 'cancel', string> keeps them.
❌ Assuming an interface catches an extra property. Excess-property checking applies only to a fresh object literal. Assign through a variable first and the extra field passes unnoticed:
const input = { id: 1, name: 'a', typo: true };
const user: User = input; // ✅ compiles — no literal, no excess check❌ Reaching for & on object shapes out of habit. A conflicting property becomes never and the error surfaces far from the declaration. extends reports it where you wrote it.
❌ Merging your own interfaces by accident. Two files declaring interface Config in the same scope quietly become one type. If that was not deliberate, use type, which errors instead.
🔑 Key Takeaways
- Only
interfacemerges across declarations; onlytypecan name a union, tuple, primitive or function. - Prefer
extendsover&for object shapes — conflicts are reported at the declaration. - A discriminated union lets the compiler prove which fields exist in each state.
?means the key may be absent;| undefinedmeans the key is required and its value may not be set.- Excess-property checking only fires on fresh object literals, not on assignment through a variable.
Interview Questions
Q: When would you use interface over type?
For object shapes, especially ones other code extends or a library user augments. extends gives clearer conflict errors, and declaration merging is the only way to add a field to a third-party type. Use type for anything that is not an object shape: unions, tuples, function signatures, mapped and conditional types.
Q: What is a discriminated union and why prefer it to optional fields?
A union whose members share a field with distinct literal types, which TypeScript uses to narrow. Optional fields describe the union of every combination, so the compiler cannot rule out { loading: true, error: 'x' }. The discriminated form makes impossible states impossible to write. Narrowing then gives you the right fields in each branch with no casting.
Q: Why did TypeScript not catch this extra property?
Excess-property checking is a special case. It only applies to an object literal assigned directly to a typed target. Assigning through a variable in between makes it a normal structural check, and extra properties pass that check. Annotate at the point of creation to keep the check.
What to Read Next
- Generics — making these shapes reusable across types
- TypeScript Advanced and Utility Types —
Omit,PickandPartialin depth - Type Guards — narrowing a discriminated union at a boundary