What are function overloads, and how do they let a single function name handle multiple different call signatures?
Short answer
You declare multiple overload signatures (no implementation) followed by one general implementation signature that's compatible with all of them; TypeScript uses the overload signatures for type-checking calls, picking the first matching one, while the implementation signature…
Advertisement
AModel answer
You declare multiple overload signatures (no implementation) followed by one general implementation signature that's compatible with all of them; TypeScript uses the overload signatures for type-checking calls, picking the first matching one, while the implementation signature is only used internally and isn't visible to callers.
function makeDate(timestamp: number): Date;
function makeDate(year: number, month: number, day: number): Date;
function makeDate(yearOrTimestamp: number, month?: number, day?: number): Date {
return month !== undefined
? new Date(yearOrTimestamp, month, day!)
: new Date(yearOrTimestamp);
}
makeDate(2026); // uses first overload
makeDate(2026, 7, 8); // uses second overloadReal-world example
Supporting both a single-timestamp and a year/month/day style call for a date-construction utility, each with precise argument-count checking.
Likely follow-ups
- What error occurs if you call the function with arguments that don't match ANY of the overload signatures?
Related topics