Distributive conditional types let you apply a condition to each item in a group separately. This helps you create flexible types that change based on each item.
Distributive conditional types in Typescript
T extends U ? X : YIf T is a union type, TypeScript applies the condition to each member separately.
This is called distributive behavior and happens only if T is a naked type parameter on the left side of extends.
string and number separately, so the result is a union of both outcomes.type Result<T> = T extends string ? "yes" : "no"; type Test = Result<string | number>; // Test is "yes" | "no"
T in a tuple disables distribution, so the whole union is checked at once.type NotDistributive<T> = [T] extends [string] ? "yes" : "no"; type Test = NotDistributive<string | number>; // Test is "no"
This program creates a type that keeps only string types from a union. It shows how distributive conditional types filter out non-string types.
type FilterStrings<T> = T extends string ? T : never; type Mixed = string | number | boolean; type OnlyStrings = FilterStrings<Mixed>; // Test with a variable const example1: OnlyStrings = "hello"; // const example2: OnlyStrings = 123; // Error: number not assignable to string console.log(example1);
Distributive conditional types only work when the checked type is a plain type parameter, not wrapped in another type like a tuple.
They are very useful for creating flexible and reusable type utilities.
Distributive conditional types apply conditions to each member of a union separately.
They help filter, map, or transform union types easily.
Wrapping the type parameter disables distribution.
Practice
T extends U ? X : Y when T is a union type?Solution
Step 1: Understand conditional types with unions
WhenTis a union, TypeScript distributes the condition over each member separately.Step 2: Apply distributive behavior
Each member ofTis checked againstUindividually, returningXorYper member.Final Answer:
Applies the condition separately to each member of the union type -> Option AQuick Check:
Distributive conditional types = separate checks per union member [OK]
- Thinking the condition applies once to the whole union
- Assuming it always returns true or false branch
- Confusing distributive with non-distributive conditional types
Solution
Step 1: Recall correct conditional type syntax
Conditional types useT extends U ? X : Ysyntax in TypeScript.Step 2: Match syntax with options
type Result<T> = T extends string ? number : boolean; matches the correct syntax; others use invalid or incorrect syntax.Final Answer:
type Result<T> = T extends string ? number : boolean; -> Option DQuick Check:
Conditional types useextends ? :syntax [OK]
extends ? : pattern in type definitions [OK]- Using if-else instead of conditional operator
- Missing the extends keyword
- Using arrow functions incorrectly in type definitions
type FilterString<T> = T extends string ? T : never;
type Result = FilterString<'a' | 1 | 'b' | true>;
What is the type of
Result?Solution
Step 1: Apply distributive conditional type to each union member
Each member is checked: 'a' extends string (true), 1 extends string (false), 'b' extends string (true), true extends string (false).Step 2: Collect true branch results
Only 'a' and 'b' remain; others become never and are removed from union.Final Answer:
'a' | 'b' -> Option CQuick Check:
FilterString filters strings only = 'a' | 'b' [OK]
- Thinking the whole union is returned unchanged
- Confusing never with empty string
- Assuming the result is just string instead of filtered literals
type ExtractNumber<T> = [T] extends [number] ? T : never;
type Result = ExtractNumber<'a' | 1 | 2>;
Solution
Step 1: Understand tuple wrapping effect
WrappingTin a tuple disables distributive behavior in conditional types.Step 2: Analyze impact on
Since distribution is off, the whole union is checked at once, soExtractNumber'a' | 1 | 2extendsnumberis false, returning never.Final Answer:
The condition is wrapped in a tuple, so distribution does not occur -> Option AQuick Check:
Tuple wrapping disables distributive conditional types [OK]
- Assuming distribution still happens with tuple wrapping
- Confusing syntax error with logic error
- Thinking union types are not allowed in conditional types
T into a union of their array types using distributive conditional types?Choose the correct type definition.
Solution
Step 1: Understand distributive conditional type for mapping
UsingT extends any ? T[] : neverapplies the array transformation to each union member separately.Step 2: Compare options
type ToArray<T> = T extends any ? T[] : never; correctly distributes and converts each member to an array type. type ToArray<T> = [T] extends [any] ? T[] : never; disables distribution by tuple wrapping. type ToArray<T> = T[]; wraps the whole union as an array, not each member. type ToArray<T> = T extends any[] ? T : never; filters arrays, not converts.Final Answer:
type ToArray<T> = T extends any ? T[] : never; -> Option BQuick Check:
Distributive conditional types map unions member-wise [OK]
T extends any ? to distribute over unions [OK]- Wrapping
Tin tuple disables distribution - Using
T[]directly converts whole union, not members - Confusing filtering with mapping in conditional types
Ready to prove it?
Pass the test and this pattern moves unknown
