TypeScript Tutorial · TypeScript Basics · Lesson 11 of 25

TypeScript Function Overloads

Give one function several call signatures.

Overload signatures

Sometimes a function's return type depends on its argument type. Overloads list the allowed call signatures above one implementation:

Example 1Try it yourself
function parse(value: string): number;
function parse(value: string[]): number[];
function parse(value: string | string[]): number | number[] {
  return Array.isArray(value) ? value.map(Number) : Number(value);
}

const one = parse("42");          // number
const many = parse(["1", "2"]);   // number[]
console.log(one + 1, many.map((n) => n * 10));

Output

43 [ 10, 20 ]

Callers only see the overload signatures, not the implementation signature. Often a union parameter or a generic is simpler — reach for overloads when the input/output relationship can't be expressed otherwise.

Test yourself

How many implementations does an overloaded TypeScript function have?

  1. One
  2. One per signature
  3. Two
  4. None
Show answer

Answer: One