Logo

Home

About

Blog

Contact

Guestbook

Merch/Store

Portfolio

Privacy

TOS

Click to navigate

  1. Home
  2. Joshua R. Lehman's Blog
  3. Function Overloading in TypeScript

Table of contents

  • Share on X
  • Discuss on X

Related Articles

Typing Functions: Parameters and Returns
TypeScript
8m
Dec 18, 2025

Typing Functions: Parameters and Returns

Functions are the building blocks of any program. In TypeScript, typing function parameters and return values helps catch errors early and improves code readability. This guide covers function signatures, typing parameters and returns, void functions, and implicit return types.

#TypeScript#Functions+4
Optional, Default, and Rest Parameters
TypeScript
8m
Dec 23, 2025

Optional, Default, and Rest Parameters

Functions often need flexibility in their parameters. TypeScript provides optional, default, and rest parameters to make function signatures adaptable while maintaining type safety. This guide covers how to use these features effectively.

#TypeScript#Functions+4
Basic Types in TypeScript
TypeScript
9m
Nov 23, 2025

Basic Types in TypeScript

TypeScript's type system starts with fundamental building blocks: primitive types like string, number, and boolean, along with arrays and type annotations. Understanding these basic types is essential for writing type-safe code. This comprehensive guide walks you through each primitive type, shows you how to use type annotations effectively, and teaches you best practices for working with arrays and basic data structures in TypeScript.

#TypeScript#Types+5

© Joshua R. Lehman

Full Stack Developer

Crafted with passion • Built with modern web technologies

2026 • All rights reserved

Contents

  • Introduction
  • What is Function Overloading?
  • Writing Overload Signatures
  • Implementation Signature
  • Practical Examples
  • Best Practices
  • Common Pitfalls
  • Next Steps
TypeScript

Function Overloading in TypeScript

December 28, 2025•3 min read
Joshua R. Lehman
Joshua R. Lehman
Author
Function Overloading in TypeScript
Function Overloading in TypeScript

Function Overloading in TypeScript#

Function overloading lets you define multiple function signatures for a single implementation. This is useful when a function can be called with different argument types or counts, and you want TypeScript to understand these variations clearly.

What is Function Overloading?

In TypeScript, function overloading means declaring multiple function signatures (overload signatures) for a function, followed by a single implementation that handles all cases.

This allows callers to get accurate type checking and IntelliSense based on how they call the function.

Writing Overload Signatures

Overload signatures declare the different ways a function can be called:

function combine(a: string, b: string): string;
function combine(a: number, b: number): number;

These signatures declare that combine can take either two strings or two numbers.

Implementation Signature

After the overload signatures, you write a single implementation signature that covers all cases:

function combine(a: string | number, b: string | number): string | number {
  if (typeof a === "string" && typeof b === "string") {
    return a + b;
  }
  if (typeof a === "number" && typeof b === "number") {
    return a + b;
  }
  throw new Error("Invalid arguments");
}

The implementation signature is not visible to callers; only the overload signatures are.

Practical Examples

Example: Combining Strings or Numbers

function combine(a: string, b: string): string;
function combine(a: number, b: number): number;
function combine(a: string | number, b: string | number): string | number {
  if (typeof a === "string" && typeof b === "string") {
    return a + b;
  }
  if (typeof a === "number" && typeof b === "number") {
    return a + b;
  }
  throw new Error("Invalid arguments");
}
 
const result1 = combine("Hello, ", "world!"); // string
const result2 = combine(10, 20); // number

Example: Overloading with Different Parameter Counts

function formatDate(date: Date): string;
function formatDate(date: Date, locale: string): string;
function formatDate(date: Date, locale?: string): string {
  return locale ? date.toLocaleDateString(locale) : date.toLocaleDateString();
}
 
formatDate(new Date()); // uses default locale
formatDate(new Date(), "en-US"); // uses specified locale

Best Practices

  • Always declare overload signatures before the implementation.
  • Keep overload signatures as specific as possible.
  • Use the implementation signature to handle all cases safely.
  • Throw errors or handle unexpected inputs gracefully in the implementation.
  • Document each overload signature clearly.

Tip: Overloads improve developer experience by providing precise type information for different call patterns.

Common Pitfalls

  • Forgetting to declare overload signatures before the implementation.
  • Making the implementation signature too broad or too narrow.
  • Returning types in the implementation that don't match any overload.
  • Not handling all overload cases in the implementation, leading to runtime errors.

Warning: TypeScript only checks calls against overload signatures, not the implementation signature.

Next Steps

Next, learn about arrow functions and how the this context behaves differently compared to regular functions.


Questions about function overloading? Share your thoughts or examples in the comments!