Challenge - 5 Problems
Conditional Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of conditional type overload replacement
What is the output of the following TypeScript code when calling
getValue(10)?Typescript
type Result<T> = T extends number ? string : boolean; function getValue<T>(input: T): Result<T> { if (typeof input === 'number') { return (input.toString()) as Result<T>; } else { return (true) as Result<T>; } } console.log(getValue(10));
Attempts:
2 left
💡 Hint
Look at the conditional type Result and what it returns for number inputs.
✗ Incorrect
The conditional type Result returns string if T is number. Since 10 is a number, getValue returns input.toString(), which is "10".
❓ Predict Output
intermediate2:00remaining
Return type of conditional type with union
Given the code below, what is the type of
result after calling processValue('hello')?Typescript
type Process<T> = T extends string ? number : boolean; function processValue<T>(val: T): Process<T> { if (typeof val === 'string') { return val.length as Process<T>; } else { return false as Process<T>; } } const result = processValue('hello');
Attempts:
2 left
💡 Hint
Check the conditional type Process for string inputs.
✗ Incorrect
For string input, Process resolves to number, so result is of type number.
🔧 Debug
advanced2:00remaining
Identify the error in conditional type overload replacement
What error does the following TypeScript code produce?
Typescript
type Output<T> = T extends string ? number : string; function convert<T>(input: T): Output<T> { if (typeof input === 'string') { return input.length; // line A } else { return input; // line B } }
Attempts:
2 left
💡 Hint
Look at the return types and what Output expects for non-string T.
✗ Incorrect
At line B, returning input (type T) is not guaranteed to be string, but Output expects string for non-string T, causing a type error.
📝 Syntax
advanced2:00remaining
Which option correctly uses conditional types for overload replacement?
Which of the following TypeScript function signatures correctly uses conditional types to replace overloads for a function
format that returns string if input is number and number if input is string?Attempts:
2 left
💡 Hint
Check the conditional type mapping input types to output types as described.
✗ Incorrect
Option C correctly maps number input to string output and string input to number output using conditional types.
🚀 Application
expert2:30remaining
Determine the output type of a complex conditional type
Consider the following TypeScript code. What is the type of
result after calling transform(true)?Typescript
type Transform<T> = T extends string ? number : T extends number ? string : boolean; function transform<T>(input: T): Transform<T> { if (typeof input === 'string') { return input.length as Transform<T>; } else if (typeof input === 'number') { return input.toString() as Transform<T>; } else { return false as Transform<T>; } } const result = transform(true);
Attempts:
2 left
💡 Hint
Check the conditional type Transform for boolean input.
✗ Incorrect
For boolean input, Transform resolves to boolean, so result is boolean.