Challenge - 5 Problems
Conditional Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple conditional type
What is the type of
Result after this code runs?Typescript
type IsString<T> = T extends string ? "Yes" : "No"; type Result = IsString<number>;
Attempts:
2 left
💡 Hint
Check if the type
number extends string.✗ Incorrect
The conditional type checks if
T extends string. Since number does not extend string, the result is the false branch: "No".❓ Predict Output
intermediate2:00remaining
Conditional type with union types
What is the type of
Output here?Typescript
type Check<T> = T extends number ? "Number" : "Other"; type Output = Check<string | number>;
Attempts:
2 left
💡 Hint
Conditional types distribute over unions.
✗ Incorrect
When a conditional type is applied to a union, it distributes over each member. So
Check becomes Check | Check , which is "Other" | "Number".🔧 Debug
advanced2:00remaining
Identify the error in this conditional type
Which option describes the error in this code snippet?
Typescript
type Example<T> = T extends ? string : number;Attempts:
2 left
💡 Hint
Check the syntax after the 'extends' keyword.
✗ Incorrect
The conditional type syntax requires a type after 'extends'. Here, the '?' appears immediately after 'extends' without a type, causing a syntax error.
❓ Predict Output
advanced2:00remaining
Nested conditional types output
What is the type of
Result?Typescript
type Nested<T> = T extends string ? (T extends "hello" ? "Hi" : "Hello") : "Not a string"; type Result = Nested<"hello">;
Attempts:
2 left
💡 Hint
Check the inner conditional when T is "hello".
✗ Incorrect
The outer conditional checks if T extends string, which is true. Then the inner conditional checks if T extends "hello", which is true, so the result is "Hi".
❓ Predict Output
expert3:00remaining
Complex conditional type with infer keyword
What is the type of
Extracted?Typescript
type ExtractReturnType<T> = T extends (...args: any[]) => infer R ? R : never; function fn(): Promise<number> { return Promise.resolve(42); } type Extracted = ExtractReturnType<typeof fn>;
Attempts:
2 left
💡 Hint
The infer keyword extracts the return type of the function.
✗ Incorrect
The conditional type extracts the return type of the function
fn. Since fn returns Promise, that is the extracted type.