Skip to content

Lesson 9 of 14 · 18m

Overloads multiply signatures; unions keep one

What an overload list really declares, why Parameters only sees the last signature, and when a union of arguments wins.

Give a function three declared signatures plus an implementation and TypeScript will check every call against the list — that's function overloading. Give it one parameter typed A | B and you get one signature, checked once. Both model "the function accepts different things." They differ in who pays the complexity bill, and the default answer is the union.

What an overload actually is

function format(v: string): string;
function format(v: number, digits: number): string;
function format(v: string | number, digits?: number): string {
  return typeof v === "string" ? v : v.toFixed(digits);
}

The first two lines are the overload signatures — the only signatures callers can see. The third is the implementation signature, invisible to callers, checked loosely (any-ish internally, verified only against the overloads' union). Callers get errors naming the overloads; the body narrows the union manually.

The mental model to reject: "overloads make the function more precise." They multiply the contract. Inference picks the first overload that matches — order matters, and a too-broad first signature silently shadows the ones below it. Adding an overload is adding API surface, not adding a check.

Parameters and ReturnType can't see them

Here's the gotcha that bites in generic infrastructure: utility types only see the last overload signature. Parameters<typeof format> is [v: number, digits: number] — the last overload — and ReturnType follows the same rule; the implementation signature and the rest of the list are both invisible to them. Type-level tooling built on overloaded functions (document.createElement, fetch-wrapper APIs, Express's app.get/app.post chains) reads only the last declared signature. If you're writing a FirstArg<F> or a memoize-generic over an overloaded API, you'll get the last overload's contract — not the implementation's, and not the whole public contract's.

When each wins

Union parameter, same return type, one code path — union. Overloads earn their keep when the return type or arity depends on the argument's literal shape: createElement("a") should be HTMLAnchorElement, createElement("canvas") should be HTMLCanvasElement, and no union of arguments gives you that mapping. DOM APIs are overloaded everywhere for exactly this reason. Two other legitimate overload cases: genuinely different arities (fn(x) vs fn(x, opts, cb) with incompatible opts), and a signature where one branch is async and the other isn't — though even there, a conditional return type (T extends ... ? Promise<X> : X) sometimes beats two signatures.

If the return type doesn't change, the union wins on error quality alone: a call that fails a union signature says "A | B expected, got C." A call that fails overloads says "no overload matches this call" and lists every signature — worse diagnostics, more API to review, and an implementation signature you must still narrow by hand anyway. You narrow inside either way; the question is whether the caller-facing contract gains precision worth paying for.

Try this

Take a helper with three overloads where all branches return the same type. Collapse it to a union signature and compare the error messages on a deliberately bad call. Then try the reverse on a function whose return type should change with a literal argument — watch the union version fail to express it, and write the two-signature overload that does.

Pitfalls

  • Overload order is the match order. Most-specific signatures first; a catch-all on top swallows the rest.
  • The implementation signature must be compatible with every overload, but isn't checked as strictly as you'd like — it's your job to keep it honest.
  • satisfies doesn't apply to function overload lists; it validates a value against a type without widening, which solves a different problem (config literals — see the untyped-code lesson).
  • Arrow functions can't be overloaded by syntax; use an overloadable function declaration or a typed variable with multiple call signatures via an object type — and ask whether you needed it.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.