What Are Function Overloads?
You'll learn: How TypeScript lets you define multiple function signatures for the same implementation to support different parameter combinations.
The Problem They Solve
Sometimes you want to call the same function in different ways — maybe with different numbers of parameters, or parameters of different types — and you want TypeScript to understand exactly what you're doing in each case.
For example, imagine a combine function that concatenates strings or adds numbers, but you want the return type to match what you pass in. Without overloads, TypeScript might only see string | number as both input and output, losing precision.
What Function Overloads Are
Function overloads let you declare multiple signatures (the parameter and return type patterns) for a single implementation (the actual function body). You write several type-only declarations above the real function, and TypeScript uses them to understand which version of the function you're calling.
Think of it like a restaurant menu with different combo meals: each listing shows different items you can order, but they all go to the same kitchen where the chef decides what to make based on what you asked for.
The Basic Structure
You write multiple overload signatures (just the types, no body), followed by one implementation signature with the actual code:
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!`;
}
}
greet("Alice"); // ✓ matches first overload
greet(30); // ✓ matches second overload
Key Takeaway: Function overloads provide multiple type-safe ways to call the same function, giving you precise types for each usage pattern while sharing one implementation.