0
0
Typescriptprogramming~20 mins

Conditional type syntax in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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>;
A"Yes"
B"No"
Cnumber
Dstring
Attempts:
2 left
💡 Hint
Check if the type number extends string.
Predict Output
intermediate
2: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>;
A"Number" | "Other"
Bstring | number
C"Other"
D"Number"
Attempts:
2 left
💡 Hint
Conditional types distribute over unions.
🔧 Debug
advanced
2: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;
ASyntaxError: Missing type after 'extends' keyword
BTypeError: Cannot use conditional type here
CNo error, code is valid
DRuntime error: Unexpected token '?'
Attempts:
2 left
💡 Hint
Check the syntax after the 'extends' keyword.
Predict Output
advanced
2: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">;
A"Not a string"
B"Hello"
Cstring
D"Hi"
Attempts:
2 left
💡 Hint
Check the inner conditional when T is "hello".
Predict Output
expert
3: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>;
Aany
Bnumber
CPromise<number>
Dnever
Attempts:
2 left
💡 Hint
The infer keyword extracts the return type of the function.