# Function Overloads

> Defining multiple signatures for a single function.

Subject: TypeScript | Lesson 19 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/function-overloads
Previous: [Typing Functions](https://codewithanimation.in/typescript/typing-functions.md) | Next: [Typing Promises & Async](https://codewithanimation.in/typescript/typing-promises-async.md)

## Multiple Personalities

Sometimes a function can be called in entirely different ways. For example, a `getDate` function might accept a timestamp (number) OR it might accept a year, month, and day (three numbers).

Function Overloads allow you to define multiple strict type signatures for a single function, ensuring the caller gets the correct return type based on exactly which arguments they passed.

## When Unions aren't enough

Why not just use a union type like `function get(arg: string | string[])`? Because if you return a different type based on the argument (e.g., returning a single item vs an array of items), the return type becomes a messy union (`Item | Item[]`).

Overloads allow you to say definitively: 'If you pass a string, I return an Item. If you pass an array, I return an Item array.'.

## The Signature Stack

You write the overload signatures first, followed by one single implementation signature that handles all the logic.

```typescript
// 1. Signature for single ID
function getUser(id: number): User;
// 2. Signature for multiple IDs
function getUser(ids: number[]): User[];

// 3. The Implementation (must cover all cases)
function getUser(idOrIds: number | number[]): User | User[] {
  if (Array.isArray(idOrIds)) {
    return idOrIds.map(fetchUser);
  }
  return fetchUser(idOrIds);
}
```

## Common mistake: The hidden implementation

A huge point of confusion: The implementation signature (the one with the actual code block) is **invisible** to the caller.

If your implementation signature says `(arg: any)`, the caller still cannot pass `any`. They are strictly limited to the specific overload signatures defined above it.

## Tip: Overloads vs Optional Parameters

Don't use overloads when a simple optional parameter or union type will do. Only use overloads when the *return type* changes based on the arguments, or when the combination of required arguments changes.

## Try it: Try it

Select which overload signature to invoke. Watch how the single implementation block handles the different argument types to return different outputs.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/function-overloads

## Check yourself

### Question 1

Can a caller directly invoke the final implementation signature of an overloaded function?

- A. Yes, it acts as a fallback.
- B. No, the implementation signature is strictly hidden and only the overload signatures are visible to the caller.
- C. Only if the caller casts their arguments to 'any'.

Correct answer: B

The implementation signature only exists to satisfy the TypeScript compiler internally so it can ensure your code handles all the distinct overload cases.

- Why not A: The implementation signature is completely hidden from the outside world.
- Why not C: Even then, they are technically matching an overload if one exists, or failing.

## Remember this

- Overloads are multiple type signatures stacked above a single implementation.
- Use them when the return type depends on the argument types.
- The implementation signature must broadly cover all overload cases.
- The implementation signature itself is invisible to the caller.
