0
0
Typescriptprogramming~20 mins

Nested conditional types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Conditional Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested conditional type with string and number
What is the type of Result after evaluating the following code?
Typescript
type Nested<T> = T extends string ? (T extends "hello" ? "Greeting" : "String") : (T extends number ? "Number" : "Other");
type Result = Nested<"hello">;
A"Number"
B"String"
C"Greeting"
D"Other"
Attempts:
2 left
💡 Hint
Check how the nested conditions evaluate for the literal type "hello".
Predict Output
intermediate
2:00remaining
Nested conditional type with union type
What is the type of Result after evaluating this code?
Typescript
type Nested<T> = T extends string ? (T extends "a" ? 1 : 2) : (T extends number ? 3 : 4);
type Result = Nested<"a" | number>;
A1 | 3
B1 | 2 | 3
C1 | 2 | 3 | 4
D2 | 3
Attempts:
2 left
💡 Hint
Remember conditional types distribute over unions.
🔧 Debug
advanced
2:00remaining
Identify the error in nested conditional type
What error does this code produce?
Typescript
type Nested<T> = T extends string ? (T extends number ? "Impossible" : "String") : "Not String";
type Result = Nested<42>;
AType 'number' is not assignable to type 'string'.
BType 'number' does not satisfy the constraint 'string'.
C"Impossible"
D"Not String"
Attempts:
2 left
💡 Hint
Check how the outer condition evaluates for number type.
Predict Output
advanced
2:00remaining
Complex nested conditional type with multiple branches
What is the type of Result here?
Typescript
type Nested<T> = T extends string ? (T extends "x" ? "X" : (T extends "y" ? "Y" : "Other String")) : (T extends number ? (T extends 1 ? "One" : "Other Number") : "Unknown");
type Result = Nested<"y" | 1>;
A"X" | "One"
B"Y" | "One"
C"Other String" | "Other Number"
D"Y" | "Other Number"
Attempts:
2 left
💡 Hint
Evaluate each union member separately through the nested conditions.
🧠 Conceptual
expert
3:00remaining
Understanding distributive property of nested conditional types
Given the nested conditional type below, what is the type of Result?
Typescript
type Nested<T> = T extends string ? (T extends "a" ? "A" : "Not A") : (T extends number ? (T extends 2 ? "Two" : "Not Two") : "Other");
type Result = Nested<string | 2 | boolean>;
A"A" | "Not A" | "Two" | "Other"
B"A" | "Two" | "Other"
C"A" | "Not A" | "Not Two" | "Other"
D"A" | "Not A" | "Two" | "Not Two" | "Other"
Attempts:
2 left
💡 Hint
Remember conditional types distribute over unions and nested conditions apply per member.