Interfaces
Intro to Interfaces
Interfaces are another way to define object shapes in TypeScript. They're
similar to type aliases for objects, but with some key differences.
interface User {
name: string
email: string
}
const user: User = { name: 'Alice', email: 'alice@example.com' }
Interface vs Type Alias
Both can define object shapes:
// Type alias
type User = { name: string; email: string }
// Interface
interface User {
name: string
email: string
}
Key differences:
| Feature | Type Alias | Interface |
|---|---|---|
| Object shapes | ✅ | ✅ |
| Union types | ✅ | ❌ |
| Declaration merging | ❌ | ✅ |
| Extends keyword | Use & | extends |
| Class implementation | ✅ | ✅ (preferred) |
When to Use Interfaces
- Object shapes that may be extended
- Class contracts (implements)
- Library APIs that users might extend
- When you prefer the
extendssyntax
Many teams choose one and stick with it. The most common convention is:
interfaces for objects, type aliases for everything else.
In this exercise, you'll work with interfaces and their unique features.