Object Types and InterfacesLesson 2.5
Intersection types in TypeScript combining multiple types
intersection type syntax, combining interfaces, merging type properties, conflict behavior, practical composition patterns
Intersection types
An intersection type combines multiple types into one. The resulting type has all properties from every member:
type Timestamped = {
createdAt: Date;
updatedAt: Date;
};
type User = {
id: number;
name: string;
};
type UserRecord = User & Timestamped;
const record: UserRecord = {
id: 1,
name: "alice",
createdAt: new Date(),
updatedAt: new Date()
};Composing reusable mixins
type WithId = { id: string };
type WithAudit = { createdBy: string; createdAt: Date };
type Product = WithId & WithAudit & {
name: string;
price: number;
};Property conflicts
If both types have the same property name with different types, the intersection requires both. For primitives this usually results in never:
type A = { x: string };
type B = { x: number };
type C = A & B;
// C.x is string & number = neverAvoid conflicting property names across intersected types. Use intersection to compose orthogonal concerns — IDs, timestamps, permissions — without repeating properties.
