Sub topic 10
How Intersections Distribute Over Unions
This chapter assumes you can write union types and intersection types, and that you can narrow a discriminated union with a switch on its tag. If any of that is unfamiliar, review the earlier chapters on those topics first.
The distribution rule
Intersection distributes over union, the same way multiplication distributes over addition in arithmetic:
A & (B | C) becomes (A & B) | (A & C)The compiler applies this rewrite whenever it builds an intersection that contains a union. You never opt in or out. The resulting type is always a union of intersections, and each intersection is a member you can narrow to.
The rule extends to two unions. Every member of the left union pairs with every member of the right union:
(A | B) & (C | D) becomes (A & C) | (A & D) | (B & C) | (B & D)This is a cross product. Two unions with 3 members each produce up to 9 combinations before the compiler removes the impossible ones.
Adding properties to every variant
The most common use is attaching shared properties to a union without repeating them in each member.
type Named = { name: string };
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; size: number };
// Equivalent to (Named & Circle) | (Named & Square)
type NamedShape = Named & (Circle | Square);
function label(shape: NamedShape): string {
switch (shape.kind) {
case "circle":
return `${shape.name}: circle with radius ${shape.radius}`;
case "square":
return `${shape.name}: square with size ${shape.size}`;
}
}
console.log(label({ name: "wheel", kind: "circle", radius: 3 }));
console.log(label({ name: "tile", kind: "square", size: 2 }));NamedShape is still a discriminated union, and kind is still the tag. The switch narrows shape to Named & Circle in the first case. Because both members have name, you can read it in every branch. The function is also exhaustive, so the compiler accepts it with no default branch and no trailing return.
Generic wrappers distribute too
The rule holds when the union arrives through a type parameter. A helper that adds a field to any type works on a union as you would hope:
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; size: number };
type WithId<T> = T & { id: string };
type IdentifiedShape = WithId<Circle | Square>;
// (Circle & { id: string }) | (Square & { id: string })
const shape: IdentifiedShape = { id: "s1", kind: "square", size: 4 };
if (shape.kind === "circle") {
console.log(shape.radius);
} else {
console.log(shape.size);
}Keep this in mind for later. Not every built-in helper behaves this way. Omit<Circle | Square, "kind"> is computed from keyof (Circle | Square), which contains only the keys common to both members. The result is a single object type with no variants left, so the union is lost. Intersection is a safe way to extend a union. Omit is not a safe way to shrink one.
Unions of literals: intersection as filtering
When both sides are unions of literal types, the cross product is easy to compute. Two different literals have nothing in common, so their intersection is never. Only pairs of identical literals survive.
type Size = "s" | "m" | "l";
type InStock = "m" | "l" | "xl";
type Available = Size & InStock; // "m" | "l"
const ok: Available = "m";
// @ts-expect-error "s" is not in InStock, so it is not in the intersection
const bad: Available = "s";Written out in full, the compiler computes this:
| Left | Right | Result |
|---|---|---|
| `"s"` | `"m"`, `"l"`, `"xl"` | `never` for each |
| `"m"` | `"m"` | `"m"` |
| `"m"` | `"l"`, `"xl"` | `never` |
| `"l"` | `"l"` | `"l"` |
| `"l"` | `"m"`, `"xl"` | `never` |
A union with never members drops them, because never is the empty type. What remains is "m" | "l". The result is the set intersection of the two unions. It behaves like Extract<Size, InStock>, which you will meet in the next chapter.
The same reasoning works for mixed primitives. (string | number) & (number | boolean) becomes number, because the only pair that is not empty is number & number.
Intersecting two discriminated unions
The cross product matters most when both operands are discriminated unions that share a tag. Each tag value pairs with itself. A mismatched pair has two different literal types for the same property, which cannot be satisfied.
type Left =
| { kind: "a"; x: number }
| { kind: "b"; y: number };
type Right =
| { kind: "a"; p: string }
| { kind: "b"; q: string };
type Combined = Left & Right;The compiler expands Combined into four pairs:
Two of the four pairs have kind: "a" on one side and kind: "b" on the other. The property would have to be "a" & "b", which is never. The compiler recognizes kind as a discriminant property, meaning its type is a literal. It therefore reduces the whole object type to never, not just the property. Only two members remain:
type Left =
| { kind: "a"; x: number }
| { kind: "b"; y: number };
type Right =
| { kind: "a"; p: string }
| { kind: "b"; q: string };
type Combined = Left & Right;
const v1: Combined = { kind: "a", x: 1, p: "hi" };
const v2: Combined = { kind: "b", y: 2, q: "yo" };
// @ts-expect-error kind "a" needs p, and q does not belong to that member
const v3: Combined = { kind: "a", x: 1, q: "yo" };This is a useful design technique. You can describe two independent axes of variation and let the compiler discard the combinations that make no sense. The tag acts as a join key.
When a conflict does not reduce the object
Reduction to never applies when the conflicting property is a discriminant, which means a literal or unit type. For other conflicts the object type survives, and only the property becomes never:
type A = { id: string; label: string };
type B = { id: number; label: string };
type AB = A & B; // not never. Property id has type string & number, which is never.
declare const value: AB;
const label: string = value.label; // still readable
const id: never = value.id;The practical consequence is that AB is a type no real value can inhabit, because you could never construct a value with a valid id. The compiler does not warn you at the declaration. The error shows up later, when you try to build a value or when a property read gives never. After combining object types, check that the shared property types are compatible, or give conflicting properties literal types so the mismatch collapses visibly.
Subtype absorption
When one side of an intersection is already a subtype of the other, the result stays close to the narrower type. For literal unions this is exact:
type Primary = "red" | "green" | "blue";
type Warm = "red";
type Result = Primary & Warm; // "red"For object types the compiler does not always collapse the result to one named type, but the value space is the same. (A | B) & A is A | (A & B). Every value of A & B is also a value of A, so the union accepts exactly the values of A. Hover text may show the longer form, and you can ignore the difference.
Cost and limits
The cross product grows multiplicatively. A union with 30 members intersected with another union of 30 members asks the compiler to consider 900 pairs, and each pair must be checked for reduction to never. Chains of three or four large unions can multiply further. The compiler stops at a fixed ceiling, around 100,000 members, with the error "Expression produces a union type that is too complex to represent."
You will rarely hit that limit by hand, but it can appear when generic helpers intersect large unions, such as event maps or generated API types. If you see slow type checking or that error, the usual fixes are:
- Intersect fewer union operands, or intersect one object with a union instead of two unions.
- Replace one union with a single object type indexed by the tag, such as a mapped type over the union of tag values.
- Use
Extractto filter a union down first, then combine.
Summary of the rules
| Expression | Result | Column | Column | Column | Column | Column |
|---|---|---|---|---|---|---|
| `A & (B \ | C)` | `(A & B) \ | (A & C)` | |||
| `(A \ | B) & (C \ | D)` | `(A & C) \ | (A & D) \ | (B & C) \ | (B & D)` |
| `"x" & "y"` | `never` | |||||
| `{ kind: "a" } & { kind: "b" }` | `never`, because `kind` is a discriminant | |||||
| `{ id: string } & { id: number }` | An object type whose `id` is `never` | |||||
| `T & { id: string }` with `T` a union | Each member gets `id` |
Exercise
What is the type of ("a" | "b" | "c") & ("b" | "c" | "d"), and how does the compiler arrive at it?
Show answer
It is "b" | "c". The compiler distributes to nine pairs. Each pair of different literals, such as "a" & "b", reduces to never. Only "b" & "b" and "c" & "c" survive, and never members vanish from the union.
Exercise
Given type Event = { type: "click"; x: number } | { type: "key"; code: string }, write a type Timed that adds a timestamp: number property to every member of Event. Then say whether Omit<Event, "type"> would give the same kind of result.
Show answer
Write type Timed = Event & { timestamp: number };. It distributes to ({ type: "click"; x: number } & { timestamp: number }) | ({ type: "key"; code: string } & { timestamp: number }), and narrowing on type still works. Omit<Event, "type"> would not preserve the variants. It uses keyof on the union, which keeps only the common keys, so the result is a single object type with no x or code.
Exercise
For type L = { k: "a"; x: 1 } | { k: "b"; y: 2 } and type R = { k: "b"; z: 3 } | { k: "c"; w: 4 }, how many members does L & R have, and what are they?
Show answer
One. Of the four pairs, only the k: "b" pair is compatible, giving { k: "b"; y: 2 } & { k: "b"; z: 3 }. The other three pairs have different literal values for k and reduce to never.