TypeScript's function overloads let you declare multiple call signatures for a single function, giving callers precise types per input:
function double(value: string): string;
function double(value: number): number;
function double(value: boolean): boolean;
// Implementation (not visible to callers):
function double(value: string | number | boolean): string | number | boolean {
...
}
const s = double('hi'); // TypeScript infers: string ✅
const n = double(5); // TypeScript infers: number ✅
const b = double(true); // TypeScript infers: boolean ✅Without overloads, double would return string | number | boolean for every call — callers would always need to narrow.
double| Input type | Behavior | Example |
|---|---|---|
string | Repeat twice | 'hi' → 'hihi' |
number | Multiply by 2 | 5 → 10 |
boolean | Negate | true → false |
string[] | Repeat each element twice | ['a','b'] → ['aa','bb'] |
The TypeScript challenge: declare four overload signatures so each call site gets the exact return type.
Sample tests