TypeScript Interfaces vs Types
Ad
TypeScript Interfaces vs Types
Understanding the differences between interfaces and types in TypeScript and when to use each.
Interfaces
interface User {
name: string;
age: number;
email?: string;
}Type Aliases
type User = {
name: string;
age: number;
email?: string;
}Key Differences
1. Declaration Merging
Interfaces support declaration merging:
interface User {
name: string;
}
interface User {
age: number;
}
// Results in: { name: string; age: number; }2. Extending
Both can extend, but syntax differs:
// Interface
interface Admin extends User {
role: string;
}
// Type
type Admin = User & {
role: string;
}When to Use What?
Use Interfaces when:
- Defining object shapes
- Need declaration merging
- Building public APIs
Use Types when:
- Need unions or intersections
- Working with primitives
- Creating utility types
