Non-Distributive Conditionals with Tuple Wrapping
What you'll learn: How to prevent a conditional type from distributing over unions by wrapping the type parameter in brackets [T].
The Problem: Distribution You Don't Want
By default, when you pass a union type into a conditional type, TypeScript distributes the conditional over each member of that union. This is usually helpful, but sometimes you want to treat the entire union as a single entity instead.
The Solution: Tuple Wrapping
You can prevent distribution by wrapping your type parameter in brackets, turning it into a single-element tuple type. This blocks the distributive behavior:
// Distributive (normal)
type IsString<T> = T extends string ? true : false;
type DistResult = IsString<string | number>;
// Result: true | false (distributed over each member)
// Non-distributive (tuple-wrapped)
type IsStringUnion<T> = [T] extends [string] ? true : false;
type NonDistResult = IsStringUnion<string | number>;
// Result: false (treated as one whole union)
In the first example, T distributes, checking string extends string and number extends string separately, yielding true | false.
In the second, [string | number] is checked as a single tuple against [string], which fails, yielding false.
When You Need This
Use tuple wrapping when you need to:
- Check if something is exactly a type (not a union containing it)
- Perform type-level operations on the union structure itself
- Filter or transform unions as complete objects
Key Takeaway: Wrapping a type parameter in brackets [T] prevents distribution, making conditional types treat unions as single, indivisible entities instead of iterating over their members.