TypeScript Declaration Files

Module declaration file (.d.ts)

A module declaration file describes the exported contract of a separately implemented module. A matching .js file must exist for this runtime example. The declaration is not emitted as an implementation; an inaccurate declaration can still mislead callers. The example uses CommonJS output and a hand-written JS companion. Baseline: TypeScript 5.8.3. File labels are explanatory; create the files separately.

Formal Syntax & Structure

typescript syntaxtypescript
export function name(value: Type): ReturnType;

Working Example

Example Snippettypescript
// File: tsconfig.json (omit this label){  "compilerOptions": {    "target": "ES2022",    "module": "CommonJS",    "strict": true,    "types": [],    "noEmit": false,    "skipLibCheck": false,    "lib": [      "ES2022",      "DOM"    ],    "noEmitOnError": true  },  "files": [    "main.ts"  ]}
// File: dep.d.ts (omit this label)export function twice(n: number): number;
// File: dep.js (omit this label)exports.twice = n => n * 2;
// File: main.ts (omit this label)import { twice } from "./dep";console.log(twice(4));
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node main.js// Expected stdout: 8
TypeScript Reference