TypeScript Function Overloads vs. Union Parameters: Choosing the Right API

You are designing a function that accepts more than one kind of input. TypeScript gives you two obvious tools: function overloads and union parameters. Both look reasonable in a code review, and both compile. The difference shows up later, when a caller passes a value whose type is itself a union, when a return type quietly widens, or when a teammate adds a signature in the wrong position.

This guide explains how each approach works, where each one breaks, and how to choose between them. Every example assumes strict: true in your tsconfig.json, and the guidance follows the official TypeScript Handbook on functions.

Key Takeaways

  • Use a union parameter when every input produces the same kind of output. It is shorter and accepts union-typed arguments.
  • Use overloads when the return type depends on which input the caller passed, or when valid argument counts differ.
  • Consider a generic with a conditional or lookup type when overloads start repeating the same pattern.
  • Consider a discriminated union or options object when the caller must choose between named modes.
  • Overload order matters, and the implementation signature is never callable from outside.