Sub topic 2
How Conditional Types Distribute Over Unions
This chapter assumes you know TypeScript generics, union and tuple types, and the basic form of a conditional type. A conditional type has the form T extends U ? X : Y. TypeScript chooses X when T is assignable to U; otherwise, it chooses Y.
type IsString<T> = T extends string ? true : false;
type A = IsString<string>; // true
type B = IsString<number>; // falseThe important rule is that a conditional type distributes over a union when the checked type is a naked type parameter. TypeScript applies the conditional separately to each member, then joins the results into a new union.
Distribution step by step
Consider this type:
type ToArray<T> = T extends unknown ? T[] : never;
type Result = ToArray<string | number>;
// string[] | number[]TypeScript evaluates the union as if it had been written this way:
type Result = ToArray<string> | ToArray<number>;
// string[] | number[]The extends unknown condition is always true for ordinary types. It is useful here because it exposes the distribution behavior without filtering the input.
A conditional type can distribute while filtering a union:
type OnlyStrings<T> = T extends string ? T : never;
type Values = OnlyStrings<string | number | boolean>;
// stringThe results are:
OnlyStrings<string>becomesstring.OnlyStrings<number>becomesnever.OnlyStrings<boolean>becomesnever.
A union containing never simplifies because never represents an impossible value. Therefore, string | never | never becomes string.
| Input | Per-member result | Final result | Column | Column |
|---|---|---|---|---|
| `string \ | number` | `string \ | never` | `string` |
| `number \ | boolean` | `never \ | never` | `never` |
| `string \ | Date` | `string \ | never` | `string` |
Preventing distribution
Sometimes you need to test the complete union instead of testing each member. Wrap both sides of extends in a single-element tuple:
type IsUnionAssignableToString<T> =
[T] extends [string] ? true : false;
type A = IsUnionAssignableToString<string | number>; // false
type B = IsUnionAssignableToString<'ready' | 'done'>; // trueThe tuple prevents T from being a naked type parameter in the conditional check. TypeScript sees one tuple type, so it performs one comparison.
The difference matters when a utility should answer “does every member satisfy this condition?” rather than “which members satisfy it?”
type EveryMemberIsString<T> =
[T] extends [string] ? true : false;
type SomeMemberIsString<T> =
T extends string ? true : false;
type A = EveryMemberIsString<string | number>; // false
type B = SomeMemberIsString<string | number>; // true | falseSomeMemberIsString distributes, so its result is true | false. It does not reduce that union to a single Boolean conclusion.
Distribution with `infer`
Distribution is especially useful when extracting information from a union. This utility extracts the element type from each array member:
type ElementType<T> =
T extends (infer U)[] ? U : never;
type Elements = ElementType<string[] | number[]>;
// string | numberTypeScript evaluates the two array types independently, infers string from the first and number from the second, then creates the union string | number.
A common mistake is expecting an array of the whole union:
type WrongExpectation = ElementType<string[] | number[]>;
// string | number, not (string | number)[]The utility returns element types. It does not rebuild an array.
A practical design pattern
Use a distributive conditional when each union member needs its own transformation:
type ToPromise<T> =
T extends unknown ? Promise<T> : never;
type Pending = ToPromise<string | number>;
// Promise<string> | Promise<number>Use tuple wrapping when the union must be treated as one value:
type IsExactlyString<T> =
[T] extends [string]
? [string] extends [T]
? true
: false
: false;
type A = IsExactlyString<string>; // true
type B = IsExactlyString<'hello'>; // false
type C = IsExactlyString<string | number>; // falseThe two checks prevent a narrower subtype such as 'hello' from being reported as exactly string.
Exercise
What are the results of type A = OnlyStrings<'a' | 1 | 'b'> and type B = [string | number] extends [string] ? true : false?
Show answer
A is 'a' | 'b', because the conditional filters each union member. B is false, because tuple wrapping checks the complete union and number is not assignable to string.