Function overloads let you create one function that works in different ways depending on the inputs. This helps make your code clearer and easier to use.
Function overloads in Typescript
function functionName(param: Type1): ReturnType1; function functionName(param: Type2): ReturnType2; function functionName(param: Type1 | Type2): ReturnType1 | ReturnType2 { // function implementation }
Each overload signature is written without a function body.
The last function with the body handles all cases and must cover all overloads.
function greet(name: string): string; function greet(age: number): string; function greet(value: string | number): string { if (typeof value === 'string') { return `Hello, ${value}!`; } else { return `You are ${value} years old.`; } }
function double(value: number): number; function double(value: string): string; function double(value: number | string): number | string { if (typeof value === 'number') { return value * 2; } else { return value + value; } }
This program shows a function that formats strings and numbers differently using overloads.
function format(input: string): string; function format(input: number): string; function format(input: string | number): string { if (typeof input === 'string') { return `String: '${input}'`; } else { return `Number: ${input.toFixed(2)}`; } } console.log(format('hello')); console.log(format(3.14159));
Overloads improve code clarity but all logic must be inside the single implementation.
TypeScript uses the overload signatures to check calls, not the implementation signature.
Always make sure the implementation covers all overload cases to avoid runtime errors.
Function overloads let one function handle different input types clearly.
Write multiple signatures without bodies, then one implementation with all logic.
Use overloads to improve code readability and type safety.
Practice
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 AQuick Check:
Function overloads = multiple input types handled clearly [OK]
- Thinking overloads create multiple separate functions
- Believing overloads generate docs automatically
- Assuming overloads restrict to one type only
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 DQuick Check:
Overloads = multiple signatures + one implementation [OK]
- Putting function bodies in all overload declarations
- Using union types instead of overloads
- Declaring only one function signature
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!'));Solution
Step 1: Analyze overload signatures and implementation
Two overloads: one for numbers returning number, one for strings returning string. Implementation uses + operator.Step 2: Evaluate console.log calls
combine(5, 10) adds numbers: 5 + 10 = 15. combine('Hi, ', 'there!') concatenates strings: 'Hi, ' + 'there!' = 'Hi, there!'.Final Answer:
15 and 'Hi, there!' -> Option CQuick Check:
Number add = 15, String concat = 'Hi, there!' [OK]
- Thinking + always concatenates strings only
- Expecting error due to any type in implementation
- Confusing output as string '510' for numbers
function greet(name: string): string {
return 'Hello, ' + name;
}
function greet(age: number): string {
return 'You are ' + age + ' years old';
}Solution
Step 1: Review overload syntax rules
Overloads require multiple declarations without bodies, followed by one implementation with a body.Step 2: Check given code
Both functions have bodies and same name, which is invalid overload syntax.Final Answer:
Function overloads must have multiple signatures without bodies, then one implementation -> Option BQuick Check:
Overloads = signatures no body + one implementation [OK]
- Writing multiple functions with bodies for overloads
- Using different function names for overloads
- Thinking return type must be void
function parseInput(input: string): number; function parseInput(input: number): string; // Implementation here
Solution
Step 1: Understand overload signatures
Input string returns number; input number returns string.Step 2: Check each implementation
function parseInput(input: any) { if (typeof input === 'string') return Number(input); else return input.toString(); } checks input type and converts accordingly, matching overloads. Others either return input unchanged or perform invalid operations.Final Answer:
function parseInput(input: any) { if (typeof input === 'string') return Number(input); else return input.toString(); } -> Option AQuick Check:
Type check + convert = correct overload implementation [OK]
- Ignoring input type and returning input directly
- Using union types without type checks
- Returning wrong types for given inputs
Ready to prove it?
Pass the test and this pattern moves unknown
