TypeScript Advanced Declaration Authoring
.d.mts declaration format and generation
An explicit .mts 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
.mts → .mjs + .d.mtsWorking 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.mts" ]}
// 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.mts" ]}
// File: producer/index.mts (omit this label)export function triple(n: number): number { return n * 3; }
// File: node_modules/demo/package.json (omit this label){ "name": "demo", "version": "1.0.0", "main": "./index.mjs", "types": "./index.d.mts"}
// File: main.mts (omit this label)import { triple } from "demo";console.log(triple(3));
// Run: tsc -p producer --pretty false// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node main.mjs// Expected stdout: 9