Course contentsShow
← TypeScript
Lesson 269 of 1,7966. FunctionsFree lesson

Overload Syntax: Signatures vs Implementation

Learning the structure of overload signatures above a single implementation signature.

Overload Syntax: Signatures vs Implementation

You'll learn: How to structure function overloads by writing multiple signature declarations above a single implementation function.

The Two Parts of an Overload

When you write function overloads in TypeScript, you're actually writing two distinct pieces:

  1. Overload signatures — These are the public-facing "contracts" that callers see
  2. Implementation signature — This is the actual function body that handles all cases

Think of it like a restaurant menu versus the kitchen. The menu (overload signatures) shows customers what they can order in specific ways. The kitchen (implementation) must be flexible enough to prepare any item from the menu, even though diners never see how it works internally.

The Structure

// Overload signature #1
function greet(name: string): string;

// Overload signature #2
function greet(age: number): string;

// Implementation signature (not callable directly)
function greet(nameOrAge: string | number): string {
  if (typeof nameOrAge === 'string') {
    return `Hello, ${nameOrAge}!`;
  } else {
    return `You are ${nameOrAge} years old.`;
  }
}

The overload signatures (the first two lines) define what callers can use. The implementation signature must be compatible with all overload signatures above it, typically using union types to handle every possibility.

Key Rules

  • Overload signatures have no body (just semicolons)
  • The implementation signature comes last
  • Callers only see the overload signatures, never the implementation
  • The implementation must handle every case described by the overloads

Key Takeaway: Function overloads separate the public API (overload signatures) from the internal logic (implementation signature), requiring you to declare callable versions first, then provide one flexible implementation that covers all cases.