Interfaces vs. Type Aliases in TypeScript
Understanding interfaces in TypeScript: declaration merging, extending interfaces (extends), implementing in classes, and choosing between interface and type.
Interfaces vs. Type Aliases in TypeScript
Interfaces define contractual object shapes and are the foundation of object-oriented TypeScript design.
1. Defining and Extending Interfaces
Interfaces support inheritance via the extends keyword:
interface BaseEntity {
id: string;
createdAt: Date;
}
interface User extends BaseEntity {
name: string;
email: string;
role: "admin" | "member";
}
const adminUser: User = {
id: "usr_10",
name: "Alex",
email: "alex@example.com",
role: "admin",
createdAt: new Date(),
};2. Declaration Merging (Interfaces Only)
If you declare an interface multiple times with the same name, TypeScript automatically merges their declarations into a single unified interface:
// 1. Initial declaration:
interface Window {
myAppConfig: { apiUrl: string };
}
// 2. Secondary declaration (merges with above):
interface Window {
analyticsLoaded: boolean;
}
// window now has BOTH myAppConfig and analyticsLoaded!3. Interfaces vs. Type Aliases: When to Use Which?
| Feature | interface | type alias |
|---|---|---|
| Object shapes | ✅ Yes | ✅ Yes |
| Primitives / Unions / Tuples | ❌ No | ✅ Yes (type ID = string | number) |
| Inheritance | extends Interface | Intersection & (type C = A & B) |
| Declaration Merging | ✅ Yes (great for third-party library typing) | ❌ No (duplicate name throws error) |
| Performance | Slightly faster compiler caching for objects | Flexible for complex mapped types |
Best Practice Rule
- Use
interfacefor public API object contracts, library declarations, and class implementations. - Use
typefor unions, primitives, tuples, function signatures, and complex utility transformations.
Related Topics
Type Aliases in TypeScript
Creating custom type definitions with the 'type' keyword: primitives, tuples, unions, object types, and recursive types.
Generics in TypeScript
Mastering TypeScript Generics: generic functions, interfaces, type constraints (T extends object), default generic types, and real-world API wrappers.