Sub topic 10
How Intersections Distribute Over Unions
This chapter assumes you know basic TypeScript syntax, type aliases, object types, and function types. You should also understand that TypeScript checks the shape of a value rather than its class name.
An intersection combines requirements. A union offers alternatives:
A & Bmeans a value must satisfy bothAandB.A | Bmeans a value may satisfy eitherAorB.
When an intersection contains a union, TypeScript can distribute the intersection across each union member:
(A | B) & Cis equivalent in meaning to:
(A & C) | (B & C)This is similar to distribution in algebra. The C requirement applies to every possible branch.
A worked example
Suppose an event can be either a text event or an image event. Every event also needs a timestamp.
type TextEvent = {
kind: "text";
text: string;
};
type ImageEvent = {
kind: "image";
url: string;
};
type Timestamped = {
createdAt: Date;
};
type LoggedEvent = (TextEvent | ImageEvent) & Timestamped;
const message: LoggedEvent = {
kind: "text",
text: "Hello",
createdAt: new Date(),
};
const picture: LoggedEvent = {
kind: "image",
url: "photo.png",
createdAt: new Date(),
};
function describe(event: LoggedEvent): string {
if (event.kind === "text") {
return `${event.text} at ${event.createdAt.toISOString()}`;
}
return `${event.url} at ${event.createdAt.toISOString()}`;
}
console.log(describe(message));
console.log(describe(picture));LoggedEvent behaves like this:
type LoggedEventExpanded =
| (TextEvent & Timestamped)
| (ImageEvent & Timestamped);The first branch must contain kind, text, and createdAt. The second must contain kind, url, and createdAt.
The kind property still narrows the result because the distributed branches form a discriminated union.
Why distribution matters
Without distribution, it can be difficult to see which properties belong together. The following table shows the requirements:
| Type branch | Required properties |
|---|---|
| `TextEvent & Timestamped` | `kind: "text"`, `text`, `createdAt` |
| `ImageEvent & Timestamped` | `kind: "image"`, `url`, `createdAt` |
A value cannot combine properties from unrelated branches and omit the required properties from its actual branch.
For example, this value is rejected because it has the image tag but lacks the image URL:
const invalid: LoggedEvent = {
kind: "image",
createdAt: new Date(),
};Conflicting branches
Distribution also exposes conflicts. Consider two alternatives with different literal values for the same property:
type Red = { color: "red"; value: number };
type Blue = { color: "blue"; value: number };
type WithLabel = { label: string };
type ColoredValue = (Red | Blue) & WithLabel;This is equivalent to:
type ColoredValueExpanded =
| (Red & WithLabel)
| (Blue & WithLabel);A valid value needs one color, a number, and a label. The intersection adds label to both alternatives.
However, if a shared property conflicts inside one intersection, that branch may become impossible:
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; size: number };
type OnlyCircles = Circle & { kind: "square" };OnlyCircles cannot have a kind value that is both "circle" and "square". Its kind property reduces to never, so no ordinary value can satisfy the type.
Distribution and conditional types
Conditional types have a related rule. A conditional type distributes over a union when the checked type is a naked type parameter:
type ToArray<T> = T extends unknown ? T[] : never;
type SeparateArrays = ToArray<string | number>;
// string[] | number[]Each union member is processed separately. To prevent distribution, wrap the type parameter in a tuple:
type OneArray<T> = [T] extends [unknown] ? T[] : never;
type CombinedArray = OneArray<string | number>;
// (string | number)[]Use distribution when each alternative needs its own result. Turn it off when you need one type containing the entire union.
Exercise
What is the distributed form of (Admin | Guest) & Active?
Show answer
(Admin & Active) | (Guest & Active). The Active requirements apply to both possible branches.