TypeScript Advanced Declaration Authoring
.d.cts declaration format and generation
An explicit .cts source extension fixes the module format and declaration extension under NodeNext. The producer emits a matching JavaScript file and declaration; an independent consumer resolves the package output, not the source. The negative test checks argument compatibility using that generated declaration. 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
.cts → .cjs + .d.ctsWorking 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: producer/tsconfig.json (omit this label){ "compilerOptions": { "target": "ES2022", "module": "NodeNext", "strict": true, "types": [], "lib": [ "ES2022" ], "declaration": true, "outDir": "../node_modules/demo", "noEmitOnError": true }, "files": [ "index.cts" ]}
// File: producer/index.cts (omit this label)function triple(n: number): number { return n * 3; }export = triple;
// File: node_modules/demo/package.json (omit this label){ "name": "demo", "version": "1.0.0", "main": "./index.cjs", "types": "./index.d.cts"}
// File: main.cts (omit this label)import triple = require("demo");console.log(triple(3));
// Run: tsc -p producer --pretty false// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node main.cjs// Expected stdout: 9