Course contentsShow
← TypeScript
Lesson 1404 of 1,79632. Advanced PatternsFree lesson

What Function Overloads Are

Understanding how multiple function signatures allow different call patterns with distinct type checking for each.

What Function Overloads Are

You'll learn: How function overloads allow you to define multiple type signatures for the same function, giving TypeScript the ability to type-check different call patterns separately.

The Core Idea

In TypeScript, function overloads let you describe a function that can be called in multiple distinct ways, each with its own type signature. Instead of having one signature that tries to handle all possible inputs and outputs (often leading to messy union types), you can define several signatures that each precisely describe one specific calling pattern.

Think of it like a restaurant menu where the same dish name means different things depending on how you order it. "Burger" with no arguments gets you the house special, "Burger" with a string gets you a custom style, and "Burger" with an object gets you a fully customized build. Function overloads let TypeScript understand each ordering pattern separately.

How It Works

You write multiple overload signatures (declarations without implementations) followed by one implementation signature that must be compatible with all the overloads. When someone calls your function, TypeScript checks which overload signature matches their call, then uses that signature's return type.

Why You Need Them

Without overloads, you might write a function that accepts string | number and returns string | number, but TypeScript won't know that passing a string always returns a string. Overloads let you express: "If you pass a string, you get a string back. If you pass a number, you get a number back." This gives callers precise type information based on how they're calling the function.

Key Takeaway: Function overloads allow multiple function signatures for different call patterns, enabling TypeScript to perform distinct type checking for each usage rather than treating all inputs and outputs as broad unions.