What is a distributive conditional type in TypeScript?
A distributive conditional type applies the condition separately to each member of a union type. For example, T extends U ? X : Y distributes over unions in T.
Click to reveal answer
intermediate
How can you prevent distribution in a conditional type?
Wrap the type in a tuple or another container, like [T] extends [U] ? X : Y. This stops TypeScript from distributing over union members.
Click to reveal answer
intermediate
Why would you want to use non-distributive conditional types?
To check a condition on the whole union type as one unit, not on each member separately. This helps when you want to treat the union as a single type.
Click to reveal answer
beginner
What does this TypeScript code do?
type Example<T> = T extends string ? true : false;
If T is a union like 'a' | 1, it checks each member: 'a' extends string → true, 1 extends string → false. So the result is true | false.
Click to reveal answer
intermediate
How does this code behave differently?
type Example<T> = [T] extends [string] ? true : false;
It checks if the whole T (even if a union) extends string as a whole. So 'a' | 1 does NOT extend string, result is false.
Click to reveal answer
What happens when you use a conditional type like T extends U ? X : Y with a union type T?
AThe condition is checked for each member of the union separately.
BThe condition is checked once for the whole union.
CThe type always resolves to Y.
DThe type always resolves to X.
✗ Incorrect
Conditional types distribute over unions by default, so each member is checked separately.
How do you stop a conditional type from distributing over a union?
AUse the 'unknown' type.
BUse the 'never' type.
CWrap the type parameter in a tuple like [T].
DUse the 'any' type.
✗ Incorrect
Wrapping in a tuple prevents distribution by making the type a single unit.
Given type T = 'a' | 1;, what is the result of T extends string ? true : false?
Anever
Btrue
Cfalse
Dtrue | false
✗ Incorrect
The conditional distributes, so 'a' gives true and 1 gives false, combined as a union.
Given type T = 'a' | 1;, what is the result of [T] extends [string] ? true : false?
Afalse
Btrue
Ctrue | false
Dnever
✗ Incorrect
The whole union is checked as one; since 1 is not string, the condition is false.
Why is non-distributive conditional type useful?
ATo split a union into individual types.
BTo check a condition on the entire union type as a whole.
CTo always return true.
DTo convert types to strings.
✗ Incorrect
It lets you treat the union as one type instead of separate members.
Explain what distributive conditional types are and how to make a conditional type non-distributive in TypeScript.
Think about how TypeScript treats unions inside conditional types.
You got /3 concepts.
Describe a scenario where you would want to use a non-distributive conditional type instead of a distributive one.
Consider when treating a union as a single type matters.
You got /3 concepts.
Practice
(1/5)
1. What does a non-distributive conditional type in TypeScript do differently compared to a distributive conditional type?
easy
A. It converts all types to string before evaluation.
B. It always distributes the condition over each member of the union type.
C. It only works with primitive types, not unions.
D. It evaluates the condition on the entire union type as a whole, not on each member separately.
Solution
Step 1: Understand distributive conditional types
Distributive conditional types apply the condition to each member of a union type separately.
Non-distributive conditional types evaluate the condition on the entire union type as a single entity, not distributing over members.
Final Answer:
It evaluates the condition on the entire union type as a whole, not on each member separately. -> Option D
Quick Check:
Non-distributive = whole union check [OK]
Hint: Non-distributive means no splitting union, check whole type [OK]
Common Mistakes:
Thinking conditional always distributes over unions
Confusing non-distributive with type narrowing
Assuming it only works on primitives
2. Which syntax correctly defines a non-distributive conditional type in TypeScript?
easy
A. type NonDist<T> = T | string ? true : false;
B. type NonDist<T> = T extends string ? true : false;
C. type NonDist<T> = [T] extends [string] ? true : false;
D. type NonDist<T> = T & string ? true : false;
Solution
Step 1: Recognize non-distributive syntax
Wrapping the type parameter in a tuple, like [T], prevents distribution over unions.
Step 2: Compare options
type NonDist<T> = [T] extends [string] ? true : false; uses [T] extends [string], which is the correct non-distributive syntax. type NonDist<T> = T extends string ? true : false; distributes over unions. Options C and D are invalid conditional syntax.
Final Answer:
type NonDist<T> = [T] extends [string] ? true : false; -> Option C
Quick Check:
Tuple wrapping disables distribution [OK]
Hint: Wrap type in tuple [T] to prevent distribution [OK]
Common Mistakes:
Not wrapping type in tuple, causing distribution
Using invalid conditional syntax
Confusing union with intersection in condition
3. Given the code:
type Test<T> = T extends string ? 'yes' : 'no';
type Result = Test<'a' | number>;
What is the type of Result?
medium
A. 'yes' | 'no'
B. 'yes'
C. 'no'
D. Error
Solution
Step 1: Understand distributive conditional types
Since T is a union 'a' | number, the condition distributes over each member.
Step 2: Evaluate each member
'a' extends string is true, so 'yes'; number extends string is false, so 'no'. Combined result is 'yes' | 'no'.
Final Answer:
'yes' | 'no' -> Option A
Quick Check:
Distributes over union = 'yes' | 'no' [OK]
Hint: Conditional distributes over union unless wrapped [OK]
Common Mistakes:
Assuming whole union checked at once
Expecting only 'yes' or only 'no'
Confusing union with intersection
4. Identify the error in this non-distributive conditional type:
type NonDist<T> = T extends [string] ? true : false;
medium
A. It incorrectly checks if T extends a tuple containing string, not preventing distribution.
B. It correctly prevents distribution by wrapping T in a tuple.
C. It causes a syntax error because of missing brackets.
D. It always returns false regardless of T.
Solution
Step 1: Analyze the condition
The condition checks if T extends [string], a tuple type, not wrapping T.
Step 2: Understand distribution prevention
To prevent distribution, T itself must be wrapped in a tuple: [T] extends [string]. This code does not do that, so distribution is not prevented.
Final Answer:
It incorrectly checks if T extends a tuple containing string, not preventing distribution. -> Option A
Quick Check:
Wrap T, not string, to prevent distribution [OK]
Hint: Wrap T in tuple, not the type to check [OK]
Common Mistakes:
Wrapping the wrong side of extends
Confusing tuple with type wrapper
Assuming syntax error instead of logic error
5. How can you write a non-distributive conditional type that checks if a union type T is exactly string | number (no more, no less)?
hard
A. type Exact<T> = T extends string | number ? true : false;