TypeScript Advanced Declaration Authoring

Declaration overload contracts

Overloads describe distinct supported call shapes and preserve the relationship between arguments and results. The runtime implementation must handle each declared case. Avoid advertising a broad fallback when the actual library does not support it; the boolean call below is rejected with a no-overload diagnostic. Baseline: TypeScript 5.8.3, strict NodeNext consumer unless otherwise noted. Create the labeled files separately; do not paste this project into a single-file editor.

Formal Syntax & Structure

typescript syntaxtypescript
export function convert(value: string): number; export function convert(value: number): string;

Working Example

Example Snippettypescript
// File: tsconfig.json (omit this label){  "compilerOptions": {    "target": "ES2022",    "module": "NodeNext",    "moduleResolution": "NodeNext",    "strict": true,    "types": [],    "lib": [      "ES2022",      "DOM"    ],    "noEmitOnError": true  },  "files": [    "main.cts"  ]}
// File: node_modules/demo/package.json (omit this label){  "name": "demo",  "version": "1.0.0",  "main": "./index.cjs",  "types": "./index.d.cts"}
// File: node_modules/demo/index.d.cts (omit this label)export function convert(value: string): number;export function convert(value: number): string;
// File: node_modules/demo/index.cjs (omit this label)exports.convert = value => typeof value === "string" ? value.length : String(value);
// File: main.cts (omit this label)import { convert } from "demo";const n: number = convert("abc");const s: string = convert(7);console.log(n, s);
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node main.cjs// Expected stdout: 3 7
TypeScript Reference