Sub topic 2
How conditional types distribute over unions
This chapter assumes you know generics and type parameters, union types, and the basic form of a conditional type (T extends U ? X : Y). You should also be comfortable reading a mapped type.
Conditional types behave differently when the type you check is a union. TypeScript doesn't ask whether the whole union extends the target. It runs the check on each member separately and unions the results. This is called distribution. It explains why Exclude and Extract work, why never behaves strangely in conditionals, and why a type utility sometimes returns a result you didn't expect.
The rule
A conditional type is distributive when both of these are true:
- The type being checked (the part left of
extends) is a naked type parameter. "Naked" means it appears on its own, not wrapped in an array, tuple, function, or other type. - The type parameter is instantiated with a union.
Given T extends U ? X : Y with T = A | B | C, the compiler computes the following:
(A extends U ? X : Y) | (B extends U ? X : Y) | (C extends U ? X : Y)Here is the smallest useful example.
type ToArray<T> = T extends unknown ? T[] : never;
type A = ToArray<string | number>; // string[] | number[]
type B = ToArray<string>; // string[]The condition T extends unknown is always true, so it does no filtering. It only forces distribution. Each member gets its own T[], and the results are combined. Without distribution you would get (string | number)[], which is a different type: an array that mixes both kinds of element.
The block above is not a diagram; the real diagram follows.
Why Exclude and Extract work
Distribution is the mechanism behind two built-in utilities. You can write them yourself.
type MyExclude<T, U> = T extends U ? never : T;
type MyExtract<T, U> = T extends U ? T : never;
type Letters = 'a' | 'b' | 'c';
type WithoutA = MyExclude<Letters, 'a'>; // 'b' | 'c'
type OnlyAB = MyExtract<Letters, 'a' | 'b'>; // 'a' | 'b'Take MyExclude<Letters, 'a'> step by step:
'a' extends 'a'is true, so this member becomesnever.'b' extends 'a'is false, so this member stays'b'.'c' extends 'a'is false, so this member stays'c'.- The union of
never | 'b' | 'c'is'b' | 'c', becauseneverdisappears from any union.
That last point matters. Returning never from a branch removes that member from the result. Filtering a union is just a distributive conditional where the unwanted members map to never.
Filtering a discriminated union
The same pattern selects members of a discriminated union by their tag.
type Shape =
| { kind: 'circle'; radius: number }
| { kind: 'square'; size: number }
| { kind: 'triangle'; base: number; height: number };
type ByKind<U, K extends string> = U extends { kind: K } ? U : never;
type Circle = ByKind<Shape, 'circle'>;
// { kind: 'circle'; radius: number }
type Round = ByKind<Shape, 'circle' | 'square'>;
// the circle and square membersIn ByKind, the true branch returns U. Inside that branch, U refers to the single member currently being checked, not to the whole union. This is a common source of confusion, so keep it in mind when you read a distributive conditional.
Distribution happens at instantiation
A conditional type involving a type parameter stays deferred until the parameter is replaced with a real type. Distribution happens when that replacement occurs. This means you can't see it by looking at the alias alone. The alias ToArray<T> doesn't say whether it distributes. The decision comes from how T is written in the extends position.
It also means distribution only applies to type parameters. A union written directly in the check position is not distributed:
type Direct = string | number extends unknown ? 'yes' : 'no'; // 'yes'There is no type parameter here, so the whole union is checked once. For a test like this the result usually matches, but for the ToArray shape it would not.
Opting out: wrap both sides in tuples
Sometimes you want the whole union checked as one unit. Wrap the checked type and the target type in one-element tuples. The checked type is then no longer naked, so distribution doesn't apply.
type ToArrayNoDist<T> = [T] extends [unknown] ? T[] : never;
type A = ToArrayNoDist<string | number>; // (string | number)[]Compare this with the earlier ToArray<string | number>, which gave string[] | number[]. The two results differ in what they permit. You can push a number into a (string | number)[]. You cannot push a number into a string[] | number[] without first narrowing it.
A boolean example makes the difference visible, since boolean is the union true | false.
type IsString<T> = T extends string ? true : false;
type IsStringWhole<T> = [T] extends [string] ? true : false;
type R1 = IsString<string | number>; // boolean (true | false)
type R2 = IsStringWhole<string | number>; // false
type R3 = IsStringWhole<'a' | 'b'>; // trueIsString<string | number> returns boolean because one member gives true and the other gives false. If you wanted a single yes-or-no answer about the whole union, IsString is the wrong shape and IsStringWhole is right.
The never trap
never is the empty union, so it has no members. Distributing over zero members produces zero results, and the result is never. This catches people who write a "is this never?" check.
type BadIsNever<T> = T extends never ? true : false;
type GoodIsNever<T> = [T] extends [never] ? true : false;
type R1 = BadIsNever<never>; // never
type R2 = GoodIsNever<never>; // true
type R3 = GoodIsNever<string>; // falseBadIsNever<never> doesn't return false or true. It returns never, because the conditional was never evaluated for any member. The tuple wrapper in GoodIsNever stops distribution, so [never] extends [never] is checked once and is true.
The same effect shows up whenever a distributive utility is given never. For example, ToArray<never> is never, not never[].
Only the checked parameter distributes
If a conditional type has several type parameters, only the one in the check position is split. The others are passed along whole.
type Pair<T, U> = T extends string ? [T, U] : never;
type R = Pair<'a' | 1, 2 | 3>;
// ['a', 2 | 3]T splits into 'a' and 1. For 'a', the branch produces ['a', U] with U still equal to 2 | 3. For 1, the result is never. The final type is ['a', 2 | 3], not a union of two tuples. If you wanted the cross product, you would need to distribute over U as well, with a second conditional wrapped around the first.
type Cross<T, U> = T extends string
? U extends number
? [T, U]
: never
: never;
type R = Cross<'a' | 'b', 1 | 2>;
// ['a', 1] | ['a', 2] | ['b', 1] | ['b', 2]Each conditional has its own naked parameter in its check position, so both distribute.
Distribution and infer
infer works inside distributive conditionals, and the two combine naturally. When the checked type is a union, infer runs separately for each member, and the captured types are unioned in the result.
type ElementType<T> = T extends (infer U)[] ? U : never;
type A = ElementType<string[] | number[]>; // string | number
type B = ElementType<string[] | boolean>; // stringIn B, the boolean member is itself true | false. Neither matches (infer U)[], so both return never and vanish. Only string[] contributes, giving string.
Compare this with a non-distributive version.
type ElementTypeWhole<T> = [T] extends [(infer U)[]] ? U : never;
type C = ElementTypeWhole<string[] | number[]>; // string | number
type D = ElementTypeWhole<string[] | boolean>; // neverC happens to match the distributive result, since the union string[] | number[] is assignable to (string | number)[] and U is inferred as string | number. In D, the whole union string[] | boolean is not assignable to any array type, so the check fails and the result is never. Distribution lets the array member succeed while the non-array member is dropped. The non-distributive form requires every member to match.
Common errors
| Symptom | Likely cause | Fix | Column | Column |
|---|---|---|---|---|
| Result is `boolean` instead of `true` or `false` | Mixed union distributed into `true \ | false` | Wrap in `[T] extends [U]` | |
| Result is `never` for a `never` input | Distribution over the empty union | Test `[T] extends [never]` first | ||
| `T[]` becomes `A[] \ | B[]` but you wanted `(A \ | B)[]` | Distribution split the union | Use a tuple wrapper |
| Filter keeps nothing | Branch returns `never` for every member | Check which side of `extends` holds the target | ||
| Union isn't split | Checked type is not a naked parameter, such as `T[]` or `[T]` | Check `T` directly, for example `T extends unknown` |
Practice
Exercise
Given type Pick1<T, K> = K extends keyof T ? T[K] : never;, what is Pick1<{ a: string; b: number }, 'a' | 'z'>? Explain which parameter distributes and what happens to each member.
Show answer
The result is string. K is the naked checked parameter, so it distributes over 'a' and 'z'. For 'a', 'a' extends keyof T is true, so the branch gives T['a'], which is string. For 'z', 'z' extends 'a' | 'b' is false, so the branch gives never. The union string | never simplifies to string.
Exercise
Write IsUnion<T> that returns true when T is a union of two or more members and false otherwise. Use a distributive conditional with a second type parameter that holds the original union. Then say what IsUnion<never> returns for your version.
Show answer
One solution is type IsUnion<T, U = T> = [T] extends [never] ? false : T extends unknown ? ([U] extends [T] ? false : true) : never;. The default U = T saves a copy of the whole union, since U is not in the check position and is not split. For each member T, the test [U] extends [T] asks whether the whole union fits inside that single member. For a single type it does, so the answer is false. For a real union it doesn't, so the answer is true. Each member yields the same result, so the final union collapses to true or false. The first check handles never and returns false. Without it, IsUnion<never> would be never.