Challenge - 5 Problems
Conditional Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this conditional type example?
Consider this TypeScript code using conditional types. What is the type of
Result?Typescript
type IsString<T> = T extends string ? "Yes" : "No"; type Result = IsString<number>;
Attempts:
2 left
💡 Hint
Think about whether
number extends string.✗ Incorrect
The conditional type checks if
T extends string. Since number does not extend string, the type resolves to "No".🧠 Conceptual
intermediate1:30remaining
Why are conditional types useful in TypeScript?
Which of the following best explains why conditional types are needed in TypeScript?
Attempts:
2 left
💡 Hint
Think about how types can depend on other types.
✗ Incorrect
Conditional types let you create types that depend on other types, making your type definitions more dynamic and reusable.
🔧 Debug
advanced2:00remaining
What error does this conditional type cause?
Look at this TypeScript code. What error will it produce?
Typescript
type Check<T> = T extends number ? "Number" : "Other"; type Result = Check<T>;
Attempts:
2 left
💡 Hint
Check if the generic type parameter is declared.
✗ Incorrect
The code uses
T without declaring it as a generic parameter, causing a 'T is not defined' error.❓ Predict Output
advanced2:30remaining
What is the output type of this nested conditional type?
Given this TypeScript code, what is the type of
Final?Typescript
type Nested<T> = T extends string ? (T extends "hello" ? "Greeting" : "String") : "Not String"; type Final = Nested<"hello">;
Attempts:
2 left
💡 Hint
Check the inner conditional after the first condition passes.
✗ Incorrect
Since
"hello" extends string and also equals "hello", the type resolves to "Greeting".🚀 Application
expert3:00remaining
How many keys does this conditional mapped type produce?
Consider this TypeScript code. How many keys does the type
Filtered have?Typescript
type FilterByType<T, U> = {
[K in keyof T as T[K] extends U ? K : never]: T[K]
};
interface Data {
id: number;
name: string;
active: boolean;
score: number;
}
type Filtered = FilterByType<Data, number>;Attempts:
2 left
💡 Hint
Count keys in Data whose type extends number.
✗ Incorrect
Only 'id' and 'score' have type number, so Filtered has 2 keys.