ProgramGuru

Function Overloading

Understand Function Overloading

Overload signatures describe multiple supported call forms for one implementation. Callers see the overload signatures rather than the implementation signature.

What you will learn: the purpose of function overloading, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how function overloading works and where its type rules apply.
  • Predict the exact output of the function overloading example.
  • Diagnose a common issue: Do not expect the implementation signature to add new callable overload forms.

Build the mental model

An implementation often accepts a union and narrows it inside the body. Use overloads when caller-visible return types depend on distinct argument shapes; a union parameter is simpler when one return type suffices.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Function Overloading: explained visuallyInput type → number → toFixed → string → trimInput typenumber → toFixedstring → trimFollow the values through the operation
Function Overloading: Input type → number → toFixed → string → trim.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

function formatValue(value: number): string;
function formatValue(value: string): string;
function formatValue(value: number | string): string {
  return typeof value === "number" ? value.toFixed(1) : value.trim();
}
console.log(formatValue(2));
console.log(formatValue("  hello  "));

Verified output

2.0
hello

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

For 2, the first overload applies and the number branch uses toFixed(1), producing 2.0. The string argument selects the other overload and trim removes surrounding spaces.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not expect the implementation signature to add new callable overload forms. Overloads must be compatible with the one implementation.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Add an overload accepting boolean and returning "yes" or "no". Explain why one shared implementation body is still enough.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

Which signatures define supported calls from outside an overloaded function?
Choose one answer. You can try again.

Answer and explanation

The overload signatures above the implementation.

Overload signatures describe multiple supported call forms for one implementation. Callers see the overload signatures rather than the implementation signature.

Review the key idea

Function Overloading: Overload signatures describe multiple supported call forms for one implementation.

For 2, the first overload applies and the number branch uses toFixed(1), producing 2.0. The string argument selects the other overload and trim removes surrounding spaces.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Anonymous Functions →