Aliasing Intersection Types
What you'll learn: How to create reusable intersection type aliases that combine multiple types into one composite type.
What Are Intersection Type Aliases?
You already know how to create union types (string | number) and alias them with type. Now you'll learn to combine types using intersections (&), and give those combinations meaningful names.
An intersection type merges multiple types together. A value must satisfy all the types simultaneously. When you alias an intersection, you're creating a named type that represents this merged capability.
Basic Syntax
type User = {
name: string;
email: string;
};
type Permissions = {
canEdit: boolean;
canDelete: boolean;
};
type Admin = User & Permissions;
const admin: Admin = {
name: "Alice",
email: "alice@example.com",
canEdit: true,
canDelete: true
};
The Admin type requires all properties from both User and Permissions.
Why Alias Intersections?
Without aliasing, you'd write User & Permissions repeatedly. The alias makes your code clearer and more maintainable:
type Moderator = User & Permissions;
type SuperUser = User & Permissions & { level: number };
Combining Multiple Types
type Identifiable = { id: number };
type Timestamped = { createdAt: Date };
type Deletable = { isDeleted: boolean };
type AuditedEntity = Identifiable & Timestamped & Deletable;
Think of it like stacking capabilities: each & adds more required properties.
Key Takeaway: Use intersection type aliases (type Name = A & B) to compose types cleanly, combining capabilities from multiple sources into one reusable, descriptive type.