Intersections vs. Unions

What

Unions (|) and Intersections (&) are the two primary ways to combine types, but they represent fundamentally different logical relationships.

How

  • Unions (|) represent “OR.” The resulting type can be any one of the constituent types. It “widens” the set of possible values.
  • Intersections (&) represent “AND.” The resulting type must satisfy all of the constituent types. It “narrows” the set of possible values by adding more requirements.
type Human = { name: string };
type Elf = { ears: "pointy" };

type ElfOrHuman = Human | Elf;   // Must have name OR ears
type ElfAndHuman = Human & Elf; // Must have name AND ears

Why

Choosing between them depends on your goal: use a union when you have a set of distinct options or states, and use an intersection when you want to compose a complex object from simpler traits or add more required fields to an existing structure.

Summary

Unions offer choices between types, while intersections combine multiple types into a single, stricter requirement.