0
0
Typescriptprogramming~20 mins

Conditional types for overload replacement in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
ATypeError at runtime
B"10"
C10
Dtrue
Attempts:
2 left
💡 Hint
Look at the conditional type Result and what it returns for number inputs.
Predict Output
intermediate
2: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');
Anumber
Bstring
Cboolean
Dstring | number
Attempts:
2 left
💡 Hint
Check the conditional type Process for string inputs.
🔧 Debug
advanced
2: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
  }
}
ANo error, code compiles fine
BType 'string' is not assignable to type 'Output<T>' at line B
CType 'number' is not assignable to type 'Output<T>' at line A
DType 'T' is not assignable to type 'string' at line B
Attempts:
2 left
💡 Hint
Look at the return types and what Output expects for non-string T.
📝 Syntax
advanced
2: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?
Afunction format<T>(input: T): T extends string ? string : number;
Bfunction format<T>(input: T): T extends number ? number : string;
Cfunction format<T>(input: T): T extends number ? string : number;
Dfunction format<T>(input: T): T extends string ? number : string;
Attempts:
2 left
💡 Hint
Check the conditional type mapping input types to output types as described.
🚀 Application
expert
2: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);
Aboolean
Bstring
Cnumber
Dnever
Attempts:
2 left
💡 Hint
Check the conditional type Transform for boolean input.