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 conditional type with string generic
What is the output of the following TypeScript code when
TestType<'hello'> is used?Typescript
type TestType<T> = T extends string ? 'string type' : 'other type'; const result: TestType<'hello'> = 'string type'; console.log(result);
Attempts:
2 left
💡 Hint
Check if the generic type extends string or not.
✗ Incorrect
The generic type 'hello' extends string, so the conditional type resolves to 'string type'.
❓ Predict Output
intermediate2:00remaining
Conditional type with union generic
What is the type of
Result in the code below?Typescript
type Conditional<T> = T extends number ? 'number' : 'not number'; type Result = Conditional<string | number>; // What is Result?
Attempts:
2 left
💡 Hint
Conditional types distribute over unions.
✗ Incorrect
Conditional types distribute over union types, so Result becomes 'number' | 'not number'.
🔧 Debug
advanced2:00remaining
Why does this conditional type cause an error?
Identify the error in this TypeScript code and what error it produces.
Typescript
type Check<T> = T extends { id: number } ? T : never; const obj: Check<{ name: string }> = { name: 'Alice' };
Attempts:
2 left
💡 Hint
Check what happens when the generic type does not extend the condition.
✗ Incorrect
Since { name: string } does not extend { id: number }, Check<{ name: string }> becomes never, so assigning an object causes a type error.
📝 Syntax
advanced2:00remaining
Which option correctly defines a conditional generic type?
Choose the option that correctly defines a conditional type with generics to check if T is assignable to string.
Attempts:
2 left
💡 Hint
Look for correct TypeScript conditional type syntax.
✗ Incorrect
Option D uses the correct syntax: 'T extends string ? true : false'. Others have syntax errors or incomplete expressions.
🚀 Application
expert3:00remaining
What is the output of this complex conditional generic type?
Given the code below, what is the type of
Result?Typescript
type Flatten<T> = T extends Array<infer U> ? U : T; type Result = Flatten<string[] | number>;
Attempts:
2 left
💡 Hint
Remember conditional types distribute over unions and infer extracts the array element type.
✗ Incorrect
Flatten distributes over the union: Flatten is string, Flatten is number, so Result is string | number.