1. Concept library
  2. JavaScript & TypeScript
  3. Module 2: Functions and overloads

Concept

Overload signatures

Overloads declare several call shapes for one function, so the return type can depend on how it was called. The implementation signature underneath handles all of them.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain how overload signatures let a return type depend on the arguments
  2. Write a function with two overloads and an implementation signature
  3. Spot an overload set that a union parameter or a generic would replace

In practice

A few lines that show Overload signatures doing its job.

TypeScript
function parse(input: string): number;
function parse(input: string[]): number[];
function parse(input: string | string[]): number | number[] {
  return Array.isArray(input) ? input.map(Number) : Number(input);
}

const one = parse("42");          // number
const many = parse(["1", "2"]);   // number[]

The common mistake

Watch for

Reaching for overloads when a union parameter or a generic would do. Overloads are checked in order and the implementation signature is invisible to callers, so they are easy to get subtly wrong.

What to know first

Your roadmap puts these before Overload signatures, unless you already know them.

How it stays learned

Once a session shows you understand Overload signatures, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled