Function Overloading allows you to define multiple versions of a function with different parameter types or counts, while having a single implementation.
Useful when the function behaves differently based on the types or number of arguments.
✅ Basic Syntax
function func(x: string): string;
function func(x: number): number;
function func(x: any): any {
return x;
}🧠 Key Rules:
You write multiple function signatures (overloads).
Then write one actual implementation that handles all cases.
✍ Example 1: Combine Strings or Numbers
function combine(a: string, b: string): string;
function combine(a: number, b: number): number;
function combine(a: any, b: any): any {
return a + b;
}
combine("Hello, ", "World!"); // "Hello, World!"
combine(10, 20); // 30✍ Example 2: Get User by ID or Name
function getUser(id: number): string;
function getUser(name: string): string;
function getUser(param: number | string): string {
if (typeof param === "number") {
return `User with ID ${param}`;
} else {
return `User with name ${param}`;
}
}
getUser(101); // "User with ID 101"
getUser("Codeveda"); // "User with name Codeveda"✨ Why Use Overloading?
✅ Improves type safety
✅ Makes API easier to use
✅ Lets TypeScript understand different call patterns
⚠ Gotchas
The actual implementation must use union types or type guards (
typeof,instanceof) to handle the variations.Only the overload signatures are visible to the caller — not the implementation.
✅ Real-World Use Case: push() method
<div class="infographic-ts" id="infographic-ts">
<style>
.infographic-ts {
--body-font: system-ui, -apple-system, sans-serif;
--mono-font: ui-monospace, SFMono-Regular, Consolas, "Courier New", monospace;
--ed-bg: #1e1e1e;
--ed-bar: #2a2a2a;
--ed-out: #d4d4d4;
--ed-muted: #8a8a8a;
--ed-ok: #4ec9b0;
--run-bg: #2e7d46;
--run-bg-hover: #35924f;
--tok-kw: #c586c0;
--tok-type: #4ec9b0;
--tok-str: #ce9178;
--tok-cmt: #808080;
--tok-var: #ffffff;
max-width: 680px;
margin: 0 auto;
box-sizing: border-box;
background: transparent;
font-family: var(--body-font);
}
.infographic-ts * { box-sizing: border-box; }
.ts-header { font-family: 'Georgia', 'Fraunces', serif; font-weight: 700;
font-size: 1.9rem; letter-spacing: 0.01em; color: inherit; text-align: center;
margin: 0 0 1.4rem; }
.code-window { background: var(--ed-bg); border: 1px solid #2f2f2f; border-radius: 8px;
overflow: hidden; margin-bottom: 1rem; }
.code-topbar { display: flex; align-items: center; justify-content: space-between;
padding: 0.5rem 0.75rem; background: var(--ed-bar); }
.code-dots { display: flex; gap: 6px; }
.code-dot { width: 9px; height: 9px; border-radius: 50%; }
.code-dot.r { background: #ff5f57; }
.code-dot.y { background: #febc2e; }
.code-dot.g { background: #28c840; }
.code-filename { font-family: var(--mono-font); font-size: 0.74rem; color: var(--ed-muted); }
.run-btn { display: inline-flex; align-items: center; gap: 0.35rem; background: var(--run-bg);
color: #fff; border: none; border-radius: 4px; padding: 0.32rem 0.7rem; font-size: 0.74rem;
font-family: var(--body-font); font-weight: 500; cursor: pointer; }
.run-btn:hover { background: var(--run-bg-hover); }
.run-btn svg { width: 10px; height: 10px; }
.code-body { padding: 0.85rem 1.1rem; font-family: var(--mono-font); font-size: 0.8rem;
line-height: 1.65; color: var(--ed-out); white-space: pre; overflow-x: auto; }
.tok-kw { color: var(--tok-kw); }
.tok-type { color: var(--tok-type); }
.tok-str { color: var(--tok-str); }
.tok-punc { color: var(--tok-var); }
.tok-cmt { color: var(--tok-cmt); }
.tok-var { color: var(--tok-var); }
.console { padding: 0 1.1rem; max-height: 0; overflow: hidden; font-family: var(--mono-font);
font-size: 0.78rem; line-height: 1.65; transition: max-height 0.35s ease, padding 0.35s ease; }
.console.show { max-height: 260px; padding: 0 1.1rem 0.85rem; }
.console-line { color: var(--ed-out); }
.console-line.ok { color: var(--ed-ok); }
.note-line { font-family: var(--body-font); font-size: 0.8rem; color: var(--ed-muted);
padding: 0 0.2rem; margin: 0; }
</style>
<div class="code-window">
<div class="code-topbar">
<div class="code-dots"><span class="code-dot r"></span><span class="code-dot y"></span><span class="code-dot g"></span></div>
<span class="code-filename">push.ts</span>
<button class="run-btn" id="pushBtn" type="button">
<svg viewBox="0 0 24 24" fill="currentColor"><polygon points="6 3 20 12 6 21 6 3"/></svg>
run
</button>
</div>
<div class="code-body"><span class="tok-cmt">// overload signatures — the allowed ways to call push</span>
<span class="tok-kw">function</span> <span class="tok-var">push</span><span class="tok-punc">(</span><span class="tok-var">item</span><span class="tok-punc">:</span> <span class="tok-type">string</span><span class="tok-punc">):</span> <span class="tok-type">void</span><span class="tok-punc">;</span>
<span class="tok-kw">function</span> <span class="tok-var">push</span><span class="tok-punc">(</span><span class="tok-var">item</span><span class="tok-punc">:</span> <span class="tok-type">string</span><span class="tok-punc">[]):</span> <span class="tok-type">void</span><span class="tok-punc">;</span>
<span class="tok-cmt">// implementation — handles both cases internally</span>
<span class="tok-kw">function</span> <span class="tok-var">push</span><span class="tok-punc">(</span><span class="tok-var">item</span><span class="tok-punc">:</span> <span class="tok-type">string</span> <span class="tok-punc">|</span> <span class="tok-type">string</span><span class="tok-punc">[]):</span> <span class="tok-type">void</span> <span class="tok-punc">{</span>
<span class="tok-kw">if</span> <span class="tok-punc">(</span><span class="tok-var">Array</span><span class="tok-punc">.</span><span class="tok-var">isArray</span><span class="tok-punc">(</span><span class="tok-var">item</span><span class="tok-punc">))</span> <span class="tok-punc">{</span>
<span class="tok-var">item</span><span class="tok-punc">.</span><span class="tok-var">forEach</span><span class="tok-punc">(</span><span class="tok-var">i</span> <span class="tok-punc">=></span> <span class="tok-var">console</span><span class="tok-punc">.</span><span class="tok-var">log</span><span class="tok-punc">(</span><span class="tok-str">`Pushed: ${</span><span class="tok-var">i</span><span class="tok-str">}`</span><span class="tok-punc">));</span>
<span class="tok-punc">}</span> <span class="tok-kw">else</span> <span class="tok-punc">{</span>
<span class="tok-var">console</span><span class="tok-punc">.</span><span class="tok-var">log</span><span class="tok-punc">(</span><span class="tok-str">`Pushed: ${</span><span class="tok-var">item</span><span class="tok-str">}`</span><span class="tok-punc">);</span>
<span class="tok-punc">}</span>
<span class="tok-punc">}</span>
<span class="tok-var">push</span><span class="tok-punc">(</span><span class="tok-str">"file.txt"</span><span class="tok-punc">);</span>
<span class="tok-var">push</span><span class="tok-punc">([</span><span class="tok-str">"file1.txt"</span><span class="tok-punc">,</span> <span class="tok-str">"file2"</span><span class="tok-punc">]);</span></div>
<div class="console" id="pushConsole"></div>
</div>
<p class="note-line">Callers only ever see the two overload signatures — a string, or an array of strings. The combined implementation type stays internal.</p>
<script>
(function () {
var wrapper = document.currentScript.closest('.infographic-ts')
|| document.querySelector('.infographic-ts');
var root = wrapper;
function line(consoleEl, cls, text) {
var el = document.createElement('div');
el.className = 'console-line ' + (cls || '');
el.textContent = text;
consoleEl.appendChild(el);
}
var btn = root.querySelector('#pushBtn');
var c = root.querySelector('#pushConsole');
btn.addEventListener('click', function () {
c.innerHTML = '';
c.classList.add('show');
line(c, 'ok', 'Pushed: file.txt');
line(c, 'ok', 'Pushed: file1.txt');
line(c, 'ok', 'Pushed: file2');
});
})();
</script>
</div>🔥 Summary
Term | Meaning |
|---|---|
Overload signature | Declared interfaces a function can support |
Implementation | A single function with logic for all cases |
Type guard | Required to differentiate parameter types ( |
✅ Function Overload Template:
function func(param1: TypeA): ReturnTypeA;
function func(param1: TypeB): ReturnTypeB;
function func(param1: TypeA | TypeB): ReturnTypeA | ReturnTypeB {
// Implementation with type guards
}