Skip to main content

Lesson 3 of 7

Function Overloads

Function overloads let a single function have multiple call signatures with different parameter/return type combinations. TypeScript resolves which overload applies based on the arguments. Overloads are the idiomatic solution when a function's return type depends on its input type.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Function Overloads - Multiple Call Signatures

Function Overloads - Multiple Call Signatures
// ── Problem overloads solve ─────────────────────────────────
// Without overloads - return type is a union (callers must narrow):
function getTaskOrId(task: Task | number): Task | number {
  if (typeof task === "number") return task * 100;
  return task;
}
const result = getTaskOrId(42);
// result: Task | number - caller must narrow even when they know it's number

// ── With overloads - return type matches input type ────────────
// Overload signatures (external - what callers see):
function getTaskOrId(task: Task): Task;
function getTaskOrId(id: number): number;
// Implementation signature (internal - broader, must cover all overloads):
function getTaskOrId(taskOrId: Task | number): Task | number {
  if (typeof taskOrId === "number") return taskOrId * 100;
  return taskOrId;
}

const t = getTaskOrId(sampleTask);   // Type: Task (not Task | number)
const n = getTaskOrId(42);           // Type: number (not Task | number)

// ── Real-world overload: fetch wrapper ────────────────────────
async function apiFetch(path: string): Promise<unknown>;
async function apiFetch<T>(path: string, schema: ZodSchema<T>): Promise<T>;
async function apiFetch<T>(
  path: string,
  schema?: ZodSchema<T>,
): Promise<T | unknown> {
  const res = await fetch(`https://api.example.com${path}`);
  const data: unknown = await res.json();
  return schema ? schema.parse(data) : data;
}

// ── createElement-style overload ─────────────────────────────
// Different return type based on tag name (like DOM's createElement)
function createElement(tag: "input"): HTMLInputElement;
function createElement(tag: "button"): HTMLButtonElement;
function createElement(tag: "div"): HTMLDivElement;
function createElement(tag: string): HTMLElement {
  return document.createElement(tag);
}

const input  = createElement("input");   // HTMLInputElement
const button = createElement("button");  // HTMLButtonElement

// ── When to use overloads vs union types ──────────────────────
// Overloads: when RETURN TYPE depends on which input TYPE is given
// Union:     when multiple input types all return the same type
type WithOverload  = { (n: number): string; (s: string): number }; // different returns
type WithUnion     = (n: number | string) => boolean;              // same return

Common Mistakes

  • Making the implementation signature too narrow - implementation signature must be wider than ALL overload signatures. It's never called directly by TypeScript.
  • Overusing overloads when generics work better - if the return type is simply T when given T, use a generic function, not overloads.
  • Forgetting overload signatures must appear BEFORE the implementation - the implementation signature with the function body must be last, after all the overload declarations.

Tip

Tip

Practice Function Overloads in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Function Overloads

Practice Task

Note

Practice Task - (1) Write a working example of Function Overloads from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Function Overloads is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.

Key Takeaways

  • Function overloads let a single function have multiple call signatures with different parameter/return type combinations.
  • Making the implementation signature too narrow - implementation signature must be wider than ALL overload signatures. It's never called directly by TypeScript.
  • Overusing overloads when generics work better - if the return type is simply T when given T, use a generic function, not overloads.
  • Forgetting overload signatures must appear BEFORE the implementation - the implementation signature with the function body must be last, after all the overload declarations.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/7 done