TypeScript Advanced Declaration Authoring
Hybrid callable exports and attached properties
A callable JavaScript export may also carry properties. Function/namespace merging describes that shape without inventing an additional object. The namespace declaration describes properties but does not create them; the implementation attaches the version value explicitly. 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): Result; declare namespace fn { const label: string; } 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;declare namespace scale { const version: string; }export = scale;
// File: node_modules/demo/index.cjs (omit this label)function scale(n) { return n * 2; }scale.version = "1";module.exports = scale;
// File: main.cts (omit this label)import scale = require("demo");console.log(scale(5), scale.version);
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node main.cjs// Expected stdout: 10 1