Function overloads allow you to define multiple function signatures for a single function implementation, enabling different ways to call the function with different argument types or counts.
Click to reveal answer
beginner
How do you declare multiple overloads for a function in TypeScript?
You write multiple function declarations with different parameter types or counts above the actual function implementation, which has a single combined signature.
Click to reveal answer
intermediate
Why must the implementation signature be compatible with all overload signatures?
Because the implementation handles all calls, it must accept all argument types and return types declared in the overloads to avoid type errors.
Click to reveal answer
intermediate
Can function overloads have different return types in TypeScript?
Yes, overloads can specify different return types depending on the input parameters, allowing flexible function behavior.
Click to reveal answer
beginner
What happens if you call a function with arguments that don't match any overload signature?
TypeScript will show a compile-time error because the call does not match any declared overload signature.
Click to reveal answer
Which of the following is the correct way to declare function overloads in TypeScript?
AMultiple function declarations with different signatures followed by one implementation
BMultiple functions with the same name but different bodies
COne function with multiple return statements
DUsing union types in a single function signature only
✗ Incorrect
Function overloads require multiple declarations with different signatures, followed by a single implementation that handles all cases.
What must the implementation signature of an overloaded function include?
AParameters and return type compatible with all overloads
BOnly the first overload's parameters and return type
CNo parameters, just a generic return type
DDifferent parameters than the overloads
✗ Incorrect
The implementation must be compatible with all overload signatures to handle every possible call.
If a function has overloads with different return types, what does TypeScript do?
AIt ignores the return types
BIt always returns the first overload's return type
CIt infers the return type based on the called overload
DIt throws a runtime error
✗ Incorrect
TypeScript uses the overload signature matching the call to determine the return type.
What happens if you call an overloaded function with arguments that don't match any overload?
AThe function returns undefined
BThe function runs with default parameters
CThe function picks the closest overload
DTypeScript shows a compile-time error
✗ Incorrect
Calls must match one of the declared overloads or TypeScript will report an error.
Can you overload a function by changing only the return type but keeping the same parameters?
AYes, return type alone is enough
BNo, overloads must differ in parameter types or count
CYes, but only for async functions
DNo, TypeScript does not support overloads
✗ Incorrect
Overloads must differ in parameters; changing only return type is not allowed.
Explain how function overloads work in TypeScript and why they are useful.
Think about how one function can behave differently based on input.
You got /5 concepts.
Describe the rules for writing the implementation of an overloaded function in TypeScript.
The implementation must cover all declared ways to call the function.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of function overloads in TypeScript?
easy
A. To allow a single function to handle different types of input with clear signatures
B. To create multiple functions with the same name but different implementations
C. To automatically generate function documentation
D. To enforce that functions only accept one specific type
Solution
Step 1: Understand function overloads concept
Function overloads let one function have multiple type signatures to handle different input types clearly.
Step 2: Compare options with concept
Only To allow a single function to handle different types of input with clear signatures correctly describes this purpose. Other options describe different or incorrect ideas.
Final Answer:
To allow a single function to handle different types of input with clear signatures -> Option A
Quick Check:
Function overloads = multiple input types handled clearly [OK]
Hint: Overloads = multiple input types, one function name [OK]
Common Mistakes:
Thinking overloads create multiple separate functions
Believing overloads generate docs automatically
Assuming overloads restrict to one type only
2. Which of the following is the correct syntax for declaring function overloads in TypeScript?
easy
A. function foo(x: any): any; function foo(x: any) { return x; }
B. function foo(x: number | string): number | string { return x; }
C. function foo(x: number): number { } function foo(x: string): string { }
D. function foo(x: number): number; function foo(x: string): string; function foo(x: any) { return x; }
Solution
Step 1: Identify correct overload syntax
Overloads require multiple declarations without bodies, then one implementation with a body.
Step 2: Check each option
function foo(x: number): number; function foo(x: string): string; function foo(x: any) { return x; } shows multiple signatures without bodies, then one implementation with a body. Others either combine signatures or have bodies in all declarations.
Final Answer:
function foo(x: number): number; function foo(x: string): string; function foo(x: any) { return x; } -> Option D
Quick Check:
Overloads = multiple signatures + one implementation [OK]
Hint: Overloads: signatures no body, one implementation with body [OK]
Common Mistakes:
Putting function bodies in all overload declarations
Using union types instead of overloads
Declaring only one function signature
3. What will be the output of the following TypeScript code?
function combine(a: number, b: number): number;
function combine(a: string, b: string): string;
function combine(a: any, b: any) {
return a + b;
}
console.log(combine(5, 10));
console.log(combine('Hi, ', 'there!'));
medium
A. '510' and 'Hi, there!'
B. 15 and 'Hithere!'
C. 15 and 'Hi, there!'
D. Error: No matching overload
Solution
Step 1: Analyze overload signatures and implementation
Two overloads: one for numbers returning number, one for strings returning string. Implementation uses + operator.