Challenge - 5 Problems
Function Overloads Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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'));
Attempts:
2 left
💡 Hint
Check how the function handles string inputs and what it returns.
✗ Incorrect
The function overloads define that when both inputs are strings, it returns their concatenation with a space in between. So the output is "Hello World".
❓ Predict Output
intermediate2: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));
Attempts:
2 left
💡 Hint
Look at how the function handles number inputs.
✗ Incorrect
When both inputs are numbers, the function returns their sum, so 5 + 10 equals 15.
❓ Predict Output
advanced2: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));
Attempts:
2 left
💡 Hint
TypeScript overload resolution checks the call against the overload signatures, ignoring the implementation signature.
✗ Incorrect
The call combine('Hello', 5) does not match any overload signature: neither (number, number) nor (string, string). TypeScript produces a compilation error: 'No overload matches this call.'
🔧 Debug
advanced2: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);
Attempts:
2 left
💡 Hint
Check the allowed argument types in the overloads and the call.
✗ Incorrect
The function overloads only accept string or number, but the call uses a boolean, so TypeScript reports a compilation error.
🧠 Conceptual
expert2:00remaining
Why use function overloads in TypeScript?
Which of the following best explains the main benefit of using function overloads in TypeScript?
Attempts:
2 left
💡 Hint
Think about how overloads help with input types and function behavior.
✗ Incorrect
Function overloads let you define multiple ways to call a function with different input types, while keeping type safety.