Sub topic 6
Distribution over unions
This chapter assumes you can write generic type aliases with constraints and that you know what a union type is. You should also be comfortable with the basic form T extends U ? X : Y from the previous chapter. Here you will learn when a conditional type runs once and when it runs once per union member. You will also learn how to control which of the two happens.
What distribution means
A conditional type is distributive when the type being checked is a naked type parameter. A naked type parameter is a bare T on the left of extends. It is not wrapped in an array, tuple, function, or any other type.
When you instantiate a distributive conditional type with a union, TypeScript does not check the union as a whole. It splits the union into its members and runs the conditional type once per member. It then combines the results into a new union.
type ToArray<T> = T extends unknown ? T[] : never;
type D = ToArray<string | number>; // string[] | number[]You might expect (string | number)[], because string | number is assignable to unknown. That is the result you get if the check runs on the whole union. Distribution makes the check run on string and number separately:
// ToArray<string | number>
// = ToArray<string> | ToArray<number>
// = string[] | number[]Inside each branch, T refers to the single member being processed, not to the whole union. This is why T[] becomes string[] for one member and number[] for the other.
When distribution applies
Three conditions must all hold:
- The checked type (the left side of
extends) is a type parameter, such asT. - That type parameter is naked.
- The conditional type is instantiated with a union, either directly or through another alias.
If the checked type is a concrete type and not a parameter, there is nothing to distribute over. TypeScript evaluates it at once:
type Direct = string | number extends string ? "yes" : "no"; // "no"Here the union is checked as a whole. string | number is not assignable to string, so the result is "no".
The position matters. Only the left side of extends distributes. A type parameter on the right side, as in string extends T ? X : Y, is never split.
The same rule gives you Exclude and Extract
The built-in Exclude relies on distribution:
type MyExclude<T, U> = T extends U ? never : T;
type Result = MyExclude<"a" | "b" | "c", "a">; // "b" | "c"The evaluation goes like this:
// MyExclude<"a" | "b" | "c", "a">
// = ("a" extends "a" ? never : "a")
// | ("b" extends "a" ? never : "b")
// | ("c" extends "a" ? never : "c")
// = never | "b" | "c"
// = "b" | "c"A never in a union disappears, so the members that matched are filtered out. Extract<T, U> is the mirror image, T extends U ? T : never. It keeps only the members that match. Neither works without distribution. A non-distributive version would test the whole union once and return either all of it or nothing.
Distribution builds unions of objects
Distribution also lets you turn a union of strings into a union of object types. Compare the results with and without it:
type EventOf<T> = T extends string ? { type: T } : never;
type Events = EventOf<"click" | "focus">;
// { type: "click" } | { type: "focus" }
type Merged<T extends string> = { type: T };
type M = Merged<"click" | "focus">;
// { type: "click" | "focus" }Events is a discriminated union. TypeScript can narrow it by checking event.type. M is a single object whose type can be either string, so it cannot be narrowed. If you want a discriminated union, route the type through a conditional type, even a trivial one like T extends string ? ... : never. This is a common way to force distribution.
Distribution happens when the type is instantiated
TypeScript does the splitting at instantiation time, when the type parameter is replaced by a real type. A few consequences follow.
boolean is the union true | false, so it distributes too:
type IsString<T> = T extends string ? true : false;
type A = IsString<string | number>; // boolean (true | false)
type B = IsString<boolean>; // falseA is boolean because one member gave true and the other gave false. The result is not false, and it does not mean "the union is a string". If you need one answer about the whole union, you have to turn distribution off.
Homomorphic mapped types over a type parameter also distribute over unions. Here Partial<T> is applied to each member:
type U = { a: number } | { b: string };
type PU = Partial<U>; // { a?: number } | { b?: string }You get a union of two partial objects, not one object with every key optional.
Preventing distribution
To stop distribution, make the checked type something other than a naked type parameter. The usual way is to wrap both sides of extends in a one-element tuple:
type ToArray<T> = T extends unknown ? T[] : never;
type ToArrayNonDist<T> = [T] extends [unknown] ? T[] : never;
type D = ToArray<string | number>; // string[] | number[]
type N = ToArrayNonDist<string | number>; // (string | number)[][T] is not a naked type parameter, so TypeScript checks [string | number] against [unknown] as one unit. The check passes, and T[] is built from the whole union.
The wrapper changes the result of Exclude in a way that shows why the built-in version is distributive:
type NonDistExclude<T, U> = [T] extends [U] ? never : T;
type X = NonDistExclude<"a" | "b", "a">; // "a" | "b"The tuple ["a" | "b"] is not assignable to ["a"], so the check fails and the whole union is returned. Nothing was excluded.
Any wrapper works, not only tuples. You could use T[] or { value: T }, but the tuple is the convention. Keep the wrapper on both sides, because [T] extends U compares a tuple against a bare type and usually fails.
Using both modes together: IsUnion
Sometimes you need distribution to visit each member and a non-distributive check to compare it with the whole. IsUnion does this with a second type parameter that defaults to the original type:
type IsUnion<T, U = T> = T extends unknown
? [U] extends [T]
? false
: true
: never;
type U1 = IsUnion<string | number>; // true
type U2 = IsUnion<string>; // false
type U3 = IsUnion<boolean>; // trueThe default U = T captures the full union before distribution starts, because U is not the checked type and is never split. The outer T extends unknown distributes over T. For each member, the inner check asks whether the full union U still fits inside that single member T. For a real union it does not, so the result is true. For a single type it does, so the result is false. boolean counts as a union because it is true | false.
The never case
Distribution over an empty union is the most common surprise. When a distributive conditional type receives never, it returns never without evaluating either branch:
type IsNeverBad<T> = T extends never ? true : false;
type R1 = IsNeverBad<never>; // never, not trueTo test for never correctly, disable distribution:
type IsNever<T> = [T] extends [never] ? true : false;
type R2 = IsNever<never>; // true
type R3 = IsNever<string>; // falseCommon mistakes
| Symptom | Cause | Fix | Column | Column |
|---|---|---|---|---|
| `IsX<A \ | B>` returns `boolean` when you wanted one answer | The check distributed and returned `true \ | false` | Wrap both sides in a tuple: `[T] extends [X]` |
| A "does this union match" check returns `never` for `never` input | Distribution over an empty union runs no branches | Use `[T] extends [never]` before other checks | ||
| A filter keeps or drops the whole union | Tuple wrapper removed distribution by accident | Use a naked `T` on the left side | ||
| A helper returns `{ type: "a" \ | "b" }` when you wanted a union of objects | No conditional type forced distribution | Add `T extends string ? { type: T } : never` | |
| Distribution does not happen for `Foo<T[]>` or `Foo<keyof T>` as the checked type | The checked type is not a naked type parameter | Introduce a helper alias that takes the type as a plain parameter |
Decision criteria
Choose distribution when you want to transform or filter each member independently. Examples are Exclude, Extract, NonNullable, and building discriminated unions. Choose a non-distributive check when you need one answer about the union as a whole. Examples are IsNever, "is every member a string", and "does this union equal that union".
When a conditional type gives a surprising union in its result, first ask whether the left side is naked. Then check which type the alias was instantiated with.
Exercise
What is the result of each type, and why? type Pick1<T> = T extends string ? T : never; type Pick2<T> = [T] extends [string] ? T : never; type R1 = Pick1<"a" | 1 | "b">; type R2 = Pick2<"a" | 1 | "b">;
Show answer
R1 is "a" | "b". Pick1 distributes: "a" gives "a", 1 gives never, "b" gives "b", and never disappears from the union. R2 is never. The tuple wrapper turns distribution off, so TypeScript checks ["a" | 1 | "b"] against [string] as one unit. The check fails because 1 is not a string, so the false branch returns never.
Exercise
Write a type AllStrings<T> that is true when every member of the union T is a string type, and false otherwise. Then say what AllStrings<"a" | 1> and AllStrings<"a" | "b"> return.
Show answer
type AllStrings<T> = [T] extends [string] ? true : false; The tuple wrapper makes the check apply to the whole union. AllStrings<"a" | 1> is false because 1 is not a string. AllStrings<"a" | "b"> is true because both members are assignable to string. A distributive version (T extends string ? true : false) would return boolean for the mixed union, which answers a different question.