TypeScript Styleguide
Interface vs Type for Object Definitions
Two ways to define object types in TypeScript. Explore the differences between interfaces and type aliases for type definitions.
79%
Use Interface
21%
Use Type Alias
Interface
TypeScript's dedicated keyword for object contracts with structural subtyping support.
Code Example
example.ts (interface)
interface User {
id: number;
name: string;
email: string;
}
interface Admin extends User {
role: 'admin';
permissions: string[];
}Key Benefits
- Designed specifically for object shapes
- Supports declaration merging
- Better for public APIs
- Clearer intent for object contracts
- Supports 'extends' inheritance
- Structural typing built-in
Statistics
Type Alias
Flexible type aliases that can represent any type, including objects, unions, and tuples.
Code Example
example.ts (type)
type User = {
id: number;
name: string;
email: string;
};
type Admin = User & {
role: 'admin';
permissions: string[];
};Key Benefits
- Works with any type, not just objects
- Supports union and intersection types
- More flexible for complex types
- Consistent with other type definitions
- No declaration merging
- Modern alternative to interfaces
Statistics
Additional Insights
More data points to help you make an informed decision for your team.
995
repositories analyzed