Sub topic 11
Distributive Conditional Types and How to Turn Distribution Off
This chapter assumes you know type aliases, unions, intersections, generic type parameters, and basic conditional types. A conditional type chooses one type or another by checking whether one type extends another.
Conditional types can distribute over unions
A conditional type has this form:
type Result<T> = T extends SomeType ? IfTrue : IfFalse;When the checked value is a naked type parameter, TypeScript applies the conditional type to each member of a union separately. It then combines the results into a new union.
type ToArray<T> = T extends unknown ? T[] : never;
type Result = ToArray<string | number>;
// string[] | number[]TypeScript evaluates this as if you had written:
type Result = ToArray<string> | ToArray<number>;
// string[] | number[]This behavior is called distributive conditional typing. The word “distributive” means that the operation distributes across the members of a union.
The true and false branches can transform or filter each member. For example, this type keeps only string members:
type OnlyStrings<T> = T extends string ? T : never;
type Values = OnlyStrings<string | number | boolean>;
// stringThe number and boolean branches produce never. Since never disappears from a union, only string remains.
A union of arrays is different from an array of a union
These two types describe different values:
type SeparateArrays = string[] | number[];
type MixedArray = (string | number)[];SeparateArrays is either an array containing only strings or an array containing only numbers. MixedArray can contain both.
A distributive conditional type creates the first form:
type ToArray<T> = T extends unknown ? T[] : never;
type SeparateArrays = ToArray<string | number>;
// string[] | number[]To create the second form, prevent distribution before applying the array constructor.
type ToMixedArray<T> = [T] extends [unknown] ? T[] : never;
type MixedArray = ToMixedArray<string | number>;
// (string | number)[]The tuple brackets make TypeScript check the complete type parameter as one value. The type parameter is no longer naked in the extends check.
Turning distribution off
The common pattern for disabling distribution is:
type NotDistributed<T> = [T] extends [SomeType] ? IfTrue : IfFalse;The tuple does not change the type’s members. It only changes how the conditional check is performed.
For example, the distributive version tests each member:
type IsString<T> = T extends string ? true : false;
type IndividualResults = IsString<string | number>;
// true | falseThe wrapped version tests the union as a whole:
type IsEntirelyString<T> = [T] extends [string] ? true : false;
type WholeResult = IsEntirelyString<string | number>;
// falseThe complete union is not assignable to string, so the result is false.
You can also use another wrapper when it preserves the intended meaning. The tuple form is the clearest and most common:
type IsUnionAssignableToUnknown<T> =
[T] extends [unknown] ? true : false;Common errors
A frequent mistake is expecting a distributive conditional type to produce one combined container.
type WrongExpectation = ToArray<string | number>;
// string[] | number[], not (string | number)[]Another mistake is wrapping only one side of the check. To suppress distribution reliably, wrap both sides:
type WholeCheck<T> = [T] extends [string] ? "yes" : "no";Distribution happens only when the checked expression is a naked type parameter. If you write T[] extends ..., Promise<T> extends ..., or [T] extends ..., TypeScript checks the complete type instead.
> callout note > Use distributive conditional types when each union member needs a separate result. Wrap the type parameter in a tuple when the union must be checked as one complete type.
Worked example
The following type removes null and undefined from every member of a union:
type NonNullableValue<T> =
T extends null | undefined ? never : T;
type Input = string | null | number | undefined;
type Cleaned = NonNullableValue<Input>;
// string | numberThe conditional type distributes over the input. The nullable members become never, and the remaining members form the result union.
Exercise
What is the result of type Result = [string | number] extends [string] ? "yes" : "no";?
Show answer
Result is "no", because the complete union string | number is not assignable to string.