Challenge - 5 Problems
Master of Non-distributive Conditional Types
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a distributive conditional type
What is the output type of the following TypeScript code?
Typescript
type A<T> = T extends string ? "yes" : "no"; type Result = A<string | number>;
Attempts:
2 left
💡 Hint
Conditional types distribute over unions by default.
✗ Incorrect
Because conditional types distribute over unions, A becomes A | A, which is "yes" | "no".
❓ Predict Output
intermediate2:00remaining
Output of a non-distributive conditional type using tuple wrapping
What is the output type of this TypeScript code?
Typescript
type B<T> = [T] extends [string] ? "yes" : "no"; type Result = B<string | number>;
Attempts:
2 left
💡 Hint
Wrapping the type in a tuple disables distribution.
✗ Incorrect
Because T is wrapped in a tuple, the conditional type does not distribute. Since string | number is not assignable to string, the result is "no".
🔧 Debug
advanced2:00remaining
Identify the error in a non-distributive conditional type
Which option causes a TypeScript error when used as a non-distributive conditional type?
Typescript
type C<T> = T extends string ? number : boolean;Attempts:
2 left
💡 Hint
Check the syntax of tuple wrapping in conditional types.
✗ Incorrect
Option A is invalid because [T] extends string is a type mismatch; the left side is a tuple type but the right side is a string type, causing a TypeScript error.
🧠 Conceptual
advanced2:00remaining
Why use tuple wrapping in conditional types?
What is the main reason to wrap a type parameter in a tuple in a conditional type?
Attempts:
2 left
💡 Hint
Think about how distribution works in conditional types.
✗ Incorrect
Wrapping the type parameter in a tuple disables the default distribution behavior of conditional types over union types.
❓ Predict Output
expert3:00remaining
Output of a complex non-distributive conditional type with nested unions
What is the type of Result after running this TypeScript code?
Typescript
type E<T> = [T] extends ["a" | "b"] ? "match" : "no match"; type Result = E<"a" | "c">;
Attempts:
2 left
💡 Hint
Remember that tuple wrapping disables distribution and the whole union is checked at once.
✗ Incorrect
Because the entire union "a" | "c" is checked at once and it is not assignable to "a" | "b", the result is "no match".