overloads Function Overloads

Last updated: 2026-10-22

overloads Function Overloads

Overload declarations give one function several signature sheets: callers get a precise return type per argument shape while the implementation is written once. TS matches top to bottom and errors only if none fits.

GroupFunctions & Classes
KindSyntax
Strict modeNot required

ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.

📝 Syntax

function format(value: number): string; // number → amount
function format(value: Date): string;   // Date → date string
function format(value: number | Date): string { // impl signature is invisible to callers
  return value instanceof Date
    ? value.toISOString().slice(0, 10)
    : `$${value.toFixed(2)}`;
}

⚙️ Parameters

f(...): T;Declarations without bodies, written consecutively; matched in order
f(...) { }The single signature with a body; its parameters must be compatible with every overload, yet it is invisible to callers

Returns:Determined by the matched overload — each overload may declare its own return type, so callers get the precise type for their argument shape.

▶ Example

Edit the code below and click Try it to see the transpiled result:

Output:

$42.00
2026-01-15

(Runtime output after transpilation; overload signatures vanish — only the implementation runs, zero branch overhead)

💡 Notes

ℹ️ Info Only the implementation survives compilation — overload signatures are compile-time syntax with zero runtime footprint.
⚠️ Note The implementation signature is invisible to callers: its parameters must cover every overload, otherwise valid calls error against it.
💡 Tip Overloads match top-down: put specific signatures first and loose ones last, or a loose signature steals calls meant for a specific one.
More cases coming soon.

📚 Related tutorials

❓ FAQ

Q Overloads or a union parameter?
A Overloads when the argument type changes the return type (a precise sheet each); a union parameter plus narrowing when it only affects internal logic and the return is the same.
Q Why do I get "no overload matches this call"?
A Check two things: whether the arguments truly fit some overload, and whether the order makes sense (a loose signature early blocks specific ones later).
Q Can arrow functions have overloads?
A No. Overloads require function declarations; for variables, use the object-type form type Overloaded = { (x: string): A; (x: number): B }.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏