BeCuriouzBeCuriouz

Learn something new every day

🔷 TypeScript
🔗

Intersection Types: When One Type Isn't Enough

💡 The & operator builds an intersection type whose values must satisfy every combined member at once — for objects, that means all their properties are required.

In the last card you saw union types (A | B), which mean "either one type or the other." Intersection types are the mirror image: written with the & operator, A & B means "a value that is both A and B at once" — it must satisfy all the members combined.

The most common use is merging object shapes. Suppose you have two small interfaces and you want a value that carries every property from both:

ts
interface HasName { name: string }
interface HasAge { age: number }

type Person = HasName & HasAge;

const p: Person = { name: "Ada", age: 36 }; // ✅ both required

Because Person is the intersection, the object must include name and age — leaving out age is a type error. Contrast this with HasName | HasAge, which would accept an object with just name, just age, or both. Union widens what's allowed; intersection narrows it by stacking requirements.

A handy mental model: with objects, & behaves like adding features together, so more & means more required properties, not fewer. This makes intersections perfect for composing capabilities — for example, type Draggable & Resizable describes a UI element that supports every method of both.