Skip to main content

Intersections

What an intersection is

An intersection combines multiple types into one that must satisfy all of them at once. You build it with the ampersand &.

type HasName = { name: string };
type HasAge = { age: number };

type Person = HasName & HasAge;

const p: Person = { name: "Ajay", age: 23 }; // Valid: must have BOTH
const q: Person = { name: "Ravi" }; // Error: missing 'age'

Union vs intersection — the mental flip

They sound similar but are opposites:

  • Union A | B — the value is either A or B. You can only use what they share.
  • Intersection A & B — the value is both A and B merged. You get everything from both.
type A = { a: number };
type B = { b: string };

type U = A | B; // has 'a' OR 'b' — safely, neither guaranteed alone
type I = A & B; // has 'a' AND 'b' — both guaranteed

Typical use — composing capabilities

Intersections shine when you build a type by stacking small, reusable pieces:

type Timestamps = { createdAt: Date; updatedAt: Date };
type User = { id: string; name: string };

type UserRecord = User & Timestamps; // a User that also carries timestamps

Fewer members with |, more members with &. A union narrows what you can safely do; an intersection accumulates what you must provide.