Interface vs Type Alias in TypeScript
Interface vs Type Alias
Section titled “Interface vs Type Alias”Key Differences
Section titled “Key Differences”| Feature | Interface | Type Alias |
|---|---|---|
| Extension | extends keyword | Intersection & |
| Declaration merging | ✅ Yes | ❌ No |
| Union types | ❌ No | ✅ Yes |
| Primitive aliases | ❌ No | ✅ Yes |
| Tuple types | ❌ No | ✅ Yes |
| Mapped/conditional | ❌ No | ✅ Yes |
| Performance | Better for objects | Similar |
Decision Guide
Section titled “Decision Guide”// Use INTERFACE when:// 1. Defining public API shapesinterface User { name: string; email: string; }
// 2. Needing declaration merginginterface Request { user?: User }
// 3. Extending other interfacesinterface Admin extends User { role: "admin" }
// Use TYPE when:// 1. Creating union typestype Status = "active" | "inactive";
// 2. Creating intersection typestype Admin = User & { permissions: string[] };
// 3. Creating mapped/conditional typestype Readonly<T> = { readonly [K in keyof T]: T[K] };
// 4. Creating function typestype Callback = (err: Error | null) => void;
// 5. Creating tuple typestype Pair = [string, number];Interview Questions
Section titled “Interview Questions”Easy: Can you use type for object shapes?
Medium: What can type do that interface cannot?
Hard: When should you always prefer interface over type?