Overloads

One name, many shapes

One name can accept several argument shapes when each shape means the same action. Think of a formatter that takes either a number or a date string. Overloads list every supported shape up front so callers pick a valid one.

function format(value: number): string; function format(value: string): string; function format(value: number | string): string { return "v:" + value; } const a: string = format(12); const b: string = format("2026-01-04"); console.log(a); console.log(b);

v:12

v:2026-01-04

Overload signatures

Each overload signature ends with a semicolon and carries no body. The signatures sit above the real implementation like a menu. Every call must match at least one line of that menu.

function getLen(text: string): number; function getLen(list: string[]): number; function getLen(input: string | string[]): number { return input.length; } console.log(getLen("abc")); console.log(getLen(["a", "b"]));

3

2

One implementation

One broader implementation follows all the signatures and does the actual work. Its parameter types must accept every listed shape, so unions are common here. Callers never see this signature, only the overload list above.

function pick(id: number): string; function pick(name: string): string; function pick(value: number | string): string { if (typeof value === "number") { return "id-" + value; } return value; } console.log(pick(7)); console.log(pick("ada"));

id-7

ada

Prefer unions or rest first

Before writing overloads, try a union parameter or a rest parameter first. One signature with a union covers many shapes with less text. Reach for overloads when each shape needs its own return type or its own parameter count.

function format(value: number | string): string { return "v:" + value; } const a: string = format(12); const b: string = format("hi"); console.log(a); console.log(b);

v:12

v:hi

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is an overload signature?

    Answer

    A function header with no body that lists one accepted call shape

    The signature ends with a semicolon, carries no body, and appears in the list the calls are checked against.

  2. Which signature do callers of an overloaded function see?

    Answer

    The overload signatures, not the wider implementation signature

    The implementation signature must accept every shape, but it is hidden from the calls.

  3. What should you try before writing overloads?

    Answer

    A union parameter or a rest parameter

    One union or rest signature covers many shapes with less text, and overloads are for the rest.

  4. What does this program print? function format(value: number): string; function format(value: string): string; function format(value: number | string): string { return "v:" + value; } console.log(format(12));

    Answer

    v:12

    The number matches the first overload and the implementation prefixes it with v:.

  5. What does this program print? function getLen(text: string): number; function getLen(list: string[]): number; function getLen(input: string | string[]): number { return input.length; } console.log(getLen(["a", "b"]));

    Answer

    2

    The array has two items, and length counts them.

  6. What does this program print? function pick(id: number): string; function pick(name: string): string; function pick(value: number | string): string { if (typeof value === "number") { return "id-" + value; } return value; } console.log(pick(7));

    Answer

    id-7

    The number takes the first branch of the implementation, which adds the id- prefix.

  7. What does the compiler say about this program? function format(value: number): string; function format(value: boolean): string; function format(value: number | boolean): string { return "v:" + value; } console.log(format("x"));

    Answer

    error TS2769: No overload matches this call.

    The overload list has a number and a boolean shape, and a string call fits neither.

  8. What does the compiler say about this program? function getLen(text: string): number; function getLen(list: string[]): number; function getLen(input: string): number { return input.length; } console.log(getLen(["a"]));

    Answer

    error TS2394: This overload signature is not compatible with its implementation signature.

    The array overload lists a shape the implementation parameter does not accept.

  9. What does this program print? function getLen(text: string): number; function getLen(list: string[]): number; function getLen(input: string | string[]): number { return input.length; } console.log(getLen("abc"));

    Answer

    3

    The string matches the first overload, and the shared implementation returns its length.

  10. What does the compiler say about this program? function pick(id: number): string { return "id-" + id; } function pick(name: string): string { return name; } console.log(pick(1));

    Answer

    error TS2393: Duplicate function implementation.

    Two bodies under one name are not overloads, and only one implementation may exist.

Article author: Arthur Isaev