Type Aliases vs Interfaces
A Type Alias allows you to create a custom name for any data type, including primitive types, unions, tuples, and object shapes.
1. Defining Type Aliases (`type`)
// Primitive Type Alias
type ID = string | number;
// Object Type Alias
type Point = {
x: number;
y: number;
};
// Function Signature Type Alias
type Formatter = (input: string) => string;2. Intersection Types (`&`)
Combine multiple types into a single type using the & operator:
type Timestamps = {
createdAt: Date;
updatedAt: Date;
};
type UserProfile = {
id: number;
name: string;
};
// Combined Type via Intersection
type FullUser = UserProfile & Timestamps;Type Aliases vs Interfaces Comparison
| Feature | Interface (`interface`) | Type Alias (`type`) |
|---|---|---|
| Object Shapes | ✅ Supported | ✅ Supported |
| Primitives & Unions | ❌ Cannot declare primitive/union aliases | ✅ Supported (`type ID = string | number`) |
| Inheritance / Extension | Uses extends keyword | Uses Intersection (&) |
| Declaration Merging | ✅ Auto-merges duplicate interfaces | ❌ Throws duplicate identifier error |
| Best Practice Use Case | OOP, Class contracts & Public APIs | Union types, Tuples, React Props & Functions |
Next Up
Learn function return types, optional/default parameters, and function overloading.
Next Lesson: Functions & Parameters →