TypeScript Function Overloads
Overloads describe a small set of supported call shapes. One implementation must handle all of those shapes correctly; the overload declarations do not generate separate implementations.
How it works
The lookup below accepts a numeric shelf position or a textual shelf code. Different parameter types share one result type here, so a union could also work. Overloads become more useful when accepted inputs lead to different result shapes. Keep the public signatures narrow and the implementation honest. Its broader implementation signature is not an extra public overload that callers may use freely.
Run the code
Try in playground loads this source without running it. Choose Run when ready. The existing TypeScript 5.8.3 worker transpiles and executes one file; it does not perform full semantic type checking, load npm imports or provide DOM/Node APIs. Use the local compiler for the type contract.
For a full check, save the three downloads together in a new folder, run npm install, then npm run check. Use npm run build followed by npm start to execute the checked JavaScript with Node. The supplied project enables strict checking, indexed-access checks and exact optional properties.
Example
function shelfName(position: number): string;
function shelfName(code: string): string;
function shelfName(value: number | string): string {
return typeof value === "number" ? "Shelf " + value : value.toUpperCase();
}
console.log(shelfName(3));
console.log(shelfName("east"));
Expected result
Shelf 3 EAST
The runtime typeof test chooses the branch. The checker verifies calls against the two overload declarations. Use a union parameter when it explains the relationship just as well; an overload list should clarify a real contract rather than add ceremony to every function.