TypeScript Advanced Declaration Authoring
Callable CommonJS library declarations
Model a package whose exported value is itself a function, not an object containing a named function. Match module.exports with export =. The declaration supplies static argument and result contracts; the separate JavaScript performs the computation. A string argument is rejected by the consumer. 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
declare function fn(value: Type): ReturnType; export = fn;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)declare function scale(n: number): number;export = scale;
// File: node_modules/demo/index.cjs (omit this label)module.exports = n => n * 3;
// File: main.cts (omit this label)import scale = require("demo");console.log(scale(4));
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node main.cjs// Expected stdout: 12