0
0
Typescriptprogramming~20 mins

Function overloads in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Overloads Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of overloaded function with string input
What is the output of the following TypeScript code when calling combine('Hello', 'World')?
Typescript
function combine(a: number, b: number): number;
function combine(a: string, b: string): string;
function combine(a: any, b: any): any {
  if (typeof a === 'string' && typeof b === 'string') {
    return a + ' ' + b;
  }
  if (typeof a === 'number' && typeof b === 'number') {
    return a + b;
  }
}

console.log(combine('Hello', 'World'));
A"HelloWorld"
B"Hello World"
Cundefined
DTypeError at runtime
Attempts:
2 left
💡 Hint
Check how the function handles string inputs and what it returns.
Predict Output
intermediate
2:00remaining
Output of overloaded function with number input
What is the output of the following TypeScript code when calling combine(5, 10)?
Typescript
function combine(a: number, b: number): number;
function combine(a: string, b: string): string;
function combine(a: any, b: any): any {
  if (typeof a === 'string' && typeof b === 'string') {
    return a + ' ' + b;
  }
  if (typeof a === 'number' && typeof b === 'number') {
    return a + b;
  }
}

console.log(combine(5, 10));
ATypeError at runtime
B"510"
Cundefined
D15
Attempts:
2 left
💡 Hint
Look at how the function handles number inputs.
Predict Output
advanced
2:00remaining
Behavior when calling overloaded function with mixed types
What happens when you call combine('Hello', 5) with the following TypeScript code?
Typescript
function combine(a: number, b: number): number;
function combine(a: string, b: string): string;
function combine(a: any, b: any): any {
  if (typeof a === 'string' && typeof b === 'string') {
    return a + ' ' + b;
  }
  if (typeof a === 'number' && typeof b === 'number') {
    return a + b;
  }
}

console.log(combine('Hello', 5));
ACompilation error
B"Hello 5"
CTypeError at runtime
Dundefined
Attempts:
2 left
💡 Hint
TypeScript overload resolution checks the call against the overload signatures, ignoring the implementation signature.
🔧 Debug
advanced
2:00remaining
Identify the error in this overloaded function
What error will this TypeScript code produce when compiled?
Typescript
function greet(name: string): string;
function greet(age: number): string;
function greet(value: any): any {
  if (typeof value === 'string') {
    return `Hello, ${value}!`;
  } else if (typeof value === 'number') {
    return `You are ${value} years old.`;
  }
}

const result = greet(true);
AOutput: 'Hello, true!'
BRuntime error: TypeError
CCompilation error: No overload matches this call
DOutput: 'You are true years old.'
Attempts:
2 left
💡 Hint
Check the allowed argument types in the overloads and the call.
🧠 Conceptual
expert
2:00remaining
Why use function overloads in TypeScript?
Which of the following best explains the main benefit of using function overloads in TypeScript?
AThey allow a single function to have multiple type-safe signatures for different input types.
BThey automatically convert all inputs to strings before processing.
CThey enable functions to run faster by compiling to optimized machine code.
DThey prevent any runtime errors by catching all bugs during compilation.
Attempts:
2 left
💡 Hint
Think about how overloads help with input types and function behavior.