0
0
Typescriptprogramming~20 mins

Non-distributive conditional types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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
intermediate
2: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>;
Astring | number
B"yes" | "no"
C"no"
D"yes"
Attempts:
2 left
💡 Hint
Conditional types distribute over unions by default.
Predict Output
intermediate
2: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>;
A"yes"
B"yes" | "no"
C"no"
Dstring | number
Attempts:
2 left
💡 Hint
Wrapping the type in a tuple disables distribution.
🔧 Debug
advanced
2: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;
Atype D<T> = [T] extends string ? number : boolean;
Btype D<T> = T extends string ? number : boolean;
Ctype D<T> = [T] extends [string] ? number : boolean;
Dtype D<T> = [T] extends [string] ? number : boolean | string;
Attempts:
2 left
💡 Hint
Check the syntax of tuple wrapping in conditional types.
🧠 Conceptual
advanced
2: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?
ATo make the conditional type distribute over unions
BTo enforce the type parameter to be a tuple
CTo convert the type parameter into an array type
DTo prevent the conditional type from distributing over unions
Attempts:
2 left
💡 Hint
Think about how distribution works in conditional types.
Predict Output
expert
3: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">;
A"no match"
B"match" | "no match"
C"match"
Dnever
Attempts:
2 left
💡 Hint
Remember that tuple wrapping disables distribution and the whole union is checked at once.