0
0
Typescriptprogramming~20 mins

Why conditional types are needed in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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>;
A"No"
Bnumber
C"Yes"
Dstring
Attempts:
2 left
💡 Hint
Think about whether number extends string.
🧠 Conceptual
intermediate
1:30remaining
Why are conditional types useful in TypeScript?
Which of the following best explains why conditional types are needed in TypeScript?
AThey are used only for runtime value checks.
BThey allow types to change based on other types, enabling flexible and reusable type logic.
CThey replace all interfaces and classes in TypeScript.
DThey make TypeScript code run faster.
Attempts:
2 left
💡 Hint
Think about how types can depend on other types.
🔧 Debug
advanced
2: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>;
ANo error, Result is "Number"
BNo error, Result is "Other"
CError: 'T' is not defined
DError: Conditional type syntax error
Attempts:
2 left
💡 Hint
Check if the generic type parameter is declared.
Predict Output
advanced
2: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">;
A"Greeting"
B"String"
C"Not String"
Dstring
Attempts:
2 left
💡 Hint
Check the inner conditional after the first condition passes.
🚀 Application
expert
3: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>;
A1
B3
C4
D2
Attempts:
2 left
💡 Hint
Count keys in Data whose type extends number.