Function OverloadsAdvanced
1Question 1 of 1
typescriptHow do Function Overloads work in TypeScript?
Explanation & Concept
Function overloads allow defining multiple function signatures for different argument counts or types, backed by a single shared implementation function.
Code Example
typescript
// Overload signatures
function parseInput(input: string): string[];
function parseInput(input: number): number[];
// Single implementation signature (must be compatible with all overloads)
function parseInput(input: string | number): (string | number)[] {
if (typeof input === "string") {
return input.split("");
}
return [input];
}
const chars = parseInput("hello"); // Type is string[]
const nums = parseInput(42); // Type is number[]How It Works & Key Takeaways
Callers get precise return types based on their input argument types, preventing ambiguous `string[] | number[]` return unions.
Mock Test · 1 Questions · Optional
Ready for Index Signatures?
Take a quick test to evaluate yourself, or move straight to the next module.
Advanced1 questions