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

The Implementation Signature Must Be Compatible

Ensuring the implementation signature accepts all overload cases.

The Implementation Signature Must Be Compatible

You'll learn how to write a function implementation that correctly handles all the overloaded cases you've defined.

The Central Rule

When you write function overloads in TypeScript, you create multiple overload signatures that describe different ways to call the function, followed by a single implementation signature where you write the actual function body.

Here's the critical rule: the implementation signature must be compatible with every overload signature. This means the implementation must be broad enough to accept all the parameter combinations and return all the return types promised by your overloads.

Why This Matters

Think of the implementation signature as a safety net. The overloads are specific promises you make to callers ("you can call me this way, or that way"), but the actual code needs to handle every scenario you've promised.

TypeScript enforces that your implementation can actually fulfill all those promises — otherwise, your function would crash at runtime when someone uses a valid overload.

Example Pattern

// Overload 1: number in, number out
function process(input: number): number;
// Overload 2: string in, string out
function process(input: string): string;

// Implementation must handle BOTH cases
function process(input: number | string): number | string {
  if (typeof input === 'number') {
    return input * 2;
  } else {
    return input.toUpperCase();
  }
}

Notice the implementation signature accepts number | string (covering both overloads) and returns number | string (both possible return types). If you tried input: number in the implementation, TypeScript would error because it couldn't handle the string overload.

Key Takeaway: The implementation signature must be a superset of all overload signatures — broad enough to safely execute every overloaded case you've defined.