TypeScript Fundamentals
Interfaces vs Type Aliases
Interfaces and type aliases both name shapes. Interfaces are object-focused and can merge; type aliases can name any type expression, including unions, intersections, tuples, primitives, and mapped/conditional types.
- Both work for ordinary object shapes
- Interfaces support declaration merging
- Type aliases handle unions and advanced expressions
- Public extension is a design signal
- Avoid holy wars
interface User {
id: string
name: string
}
type LoadState<T> =
| { kind: "loading" }
| { kind: "success"; data: T }
| { kind: "error"; error: Error }| Need | Usually choose |
|---|---|
| Plain object model | Either |
| Public augmentable API | interface |
| Union type | type |
| Tuple | type |
| Mapped/conditional type | type |
| Declaration merging | interface |