TypeScript · Types that pay for themselves
How do function overloads work?
Short answer
Overload signatures describe several valid call shapes for one implementation. The implementation signature is not callable from outside, so each public overload must be declared explicitly.
An overloaded function has one or more signature declarations followed by a single implementation. Callers only see the signatures, not the implementation signature. That lets you describe precise input and output relationships while keeping one runtime function.
function format(value: string): string;
function format(value: number): string;
function format(value: string | number): string {
return typeof value === "number"
? value.toFixed(2)
: value.trim();
}
format(" hello ");
format(3.14159);Rules to remember
- The implementation signature must be compatible with every overload
- The implementation signature is not visible to callers
- Order matters: more specific overloads should come first
- The implementation can be broader than any single overload
- Overloads cannot be expressed with a single arrow function declaration
Overloads are useful when the return type depends on the argument type or when a function accepts a few distinct call shapes. For a simple union that always returns the same type, a single signature is easier to read and maintain.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Is the implementation signature callable from outside?
- 2Why does overload order matter?
- 3Can an overload be implemented with an arrow function?
- 4When should you use an overload instead of a union?
Say it in the interview
Function overloads describe several valid call signatures and then provide one implementation. The implementation signature is not part of the public API, so every supported call shape needs its own overload. I use overloads when the return type depends on the arguments. I keep the implementation broad enough to satisfy all the overloads and put the most specific signatures first.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live TypeScript mock interview and get scored feedback on depth, clarity and confidence.