Q30: What are Distributive Conditional Types and how can distribution be disabled?
When conditional types act on a bare generic type parameter (T), they automatically distribute across union types.
For example, IsString<A | B> evaluates as IsString<A> | IsString<B>.
To disable distribution, wrap the generic type parameter in square brackets: [T] extends [U].
Disabling Distribution via Square Brackets
type ToArrayDistributive<T> = T extends any ? T[] : never;
type Distributed = ToArrayDistributive<string | number>; // string[] | number[]
type ToArrayNonDistributive<T> = [T] extends [any] ? T[] : never;
type NonDistributed = ToArrayNonDistributive<string | number>; // (string | number)[]💡 Wrapping T in tuple brackets prevents unions from splitting during evaluation.
Key Takeaways
- Unwrapped type parameters distribute automatically across unions in conditional types.
- Wrap type parameters in tuple brackets
[T]to disable distribution.