Bird
Raised Fist0
Typescriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using conditional types in TypeScript for overload replacement?
easy
A. To enforce runtime type checking in JavaScript
B. To create multiple functions with the same name but different parameters
C. To select a type based on a condition, replacing multiple overloads with one type
D. To convert all types to strings automatically

Solution

  1. Step 1: Understand conditional types

    Conditional types allow choosing a type based on a condition, like an if-else for types.
  2. Step 2: Relate to overload replacement

    They replace multiple function overloads by selecting the correct return type based on input types.
  3. Final Answer:

    To select a type based on a condition, replacing multiple overloads with one type -> Option C
  4. Quick Check:

    Conditional types = overload replacement [OK]
Hint: Think 'if-else' but for types to replace overloads [OK]
Common Mistakes:
  • Confusing conditional types with runtime checks
  • Thinking conditional types create multiple functions
  • Believing conditional types convert types to strings
2. Which of the following is the correct syntax for a conditional type in TypeScript?
easy
A. type Result = if (T extends U) { X } else { Y };
B. type Result = T == U ? X : Y;
C. type Result = T ? U : V;
D. type Result = T extends U ? X : Y;

Solution

  1. Step 1: Recall conditional type syntax

    Conditional types use the syntax: type Result = T extends U ? X : Y;
  2. Step 2: Compare options

    type Result = T extends U ? X : Y; matches the correct syntax. Options A, C, and D use invalid or incorrect syntax.
  3. Final Answer:

    type Result = T extends U ? X : Y; -> Option D
  4. Quick Check:

    Conditional type syntax = type Result = T extends U ? X : Y; [OK]
Hint: Remember: 'extends' with '?' and ':' for conditional types [OK]
Common Mistakes:
  • Using if-else syntax inside type definitions
  • Confusing ternary operator with conditional types
  • Using '==' instead of 'extends' keyword
3. Given the code below, what is the type of result?
type IsString<T> = T extends string ? "Yes" : "No";

let result: IsString<number>;
medium
A. "Yes"
B. "No"
C. number
D. string

Solution

  1. Step 1: Analyze the conditional type

    The type IsString<T> returns "Yes" if T extends string, else "No".
  2. Step 2: Apply to number

    Since number does not extend string, the type is "No".
  3. Final Answer:

    "No" -> Option B
  4. Quick Check:

    number extends string? No [OK]
Hint: Check if type extends string; if not, result is "No" [OK]
Common Mistakes:
  • Assuming number extends string
  • Confusing type with value
  • Ignoring the conditional type logic
4. Identify the error in this conditional type usage:
type Check<T> = T extends number ? "Num" : "NotNum";

function test<T>(value: T): Check<T> {
  if (typeof value === "number") {
    return "Num";
  } else {
    return "NotNum";
  }
}
medium
A. Function return type does not match because conditional types are not resolved at runtime
B. The conditional type syntax is incorrect
C. The function parameter should be typed as number
D. The if condition should check for string instead of number

Solution

  1. Step 1: Understand conditional types vs runtime

    Conditional types exist only at compile time and cannot be used to enforce runtime return types directly.
  2. Step 2: Check function return

    The function tries to return a value matching Check<T>, but TypeScript cannot guarantee this at runtime, causing a type error.
  3. Final Answer:

    Function return type does not match because conditional types are not resolved at runtime -> Option A
  4. Quick Check:

    Conditional types = compile-time only [OK]
Hint: Remember conditional types are compile-time, not runtime checks [OK]
Common Mistakes:
  • Thinking conditional types work at runtime
  • Misusing conditional type syntax
  • Changing parameter type unnecessarily
5. How can conditional types replace multiple overloads in this example?
function format(value: string): string;
function format(value: number): string;
function format(value: any): string {
  return value.toString();
}
Choose the best conditional type signature to replace these overloads.
hard
A. function format<T>(value: T): T extends string | number ? string : never;
B. function format<T>(value: T): string;
C. function format(value: T): T extends string ? string : string;
D. function format<T>(value: T): T extends string ? string : number;

Solution

  1. Step 1: Understand original overloads

    Both string and number inputs return a string output.
  2. Step 2: Choose conditional type matching overloads

    function format<T>(value: T): T extends string | number ? string : never; restricts T to string or number and returns string for those, else never (disallowed).
  3. Step 3: Check other options

    function format<T extends string | number>(value: T): T extends string ? string : string;'s return type is redundant; function format<T>(value: T): string; allows any type; function format<T>(value: T): T extends string ? string : number; returns number incorrectly.
  4. Final Answer:

    function format<T>(value: T): T extends string | number ? string : never; -> Option A
  5. Quick Check:

    Conditional type restricts inputs and return string [OK]
Hint: Use conditional type to restrict inputs and unify return type [OK]
Common Mistakes:
  • Allowing any input type without restriction
  • Returning wrong types for some inputs
  • Ignoring the need to exclude unsupported types
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard