TypeScript Advanced Declaration Authoring

ES module declaration exports

An ES module declaration must match the runtime default and named exports. A default export is a specific export binding rather than the whole CommonJS module object. The .mjs companion is loaded by a Node ESM consumer; a missing named export is rejected statically. 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
export default function fn(value: Type): Result; export const version: string;

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.mts"  ]}
// File: node_modules/demo/package.json (omit this label){  "name": "demo",  "version": "1.0.0",  "main": "./index.mjs",  "types": "./index.d.mts"}
// File: node_modules/demo/index.d.mts (omit this label)export default function scale(n: number): number;export const version: string;
// File: node_modules/demo/index.mjs (omit this label)export default n => n * 2;export const version = "2";
// File: main.mts (omit this label)import scale, { version } from "demo";console.log(scale(3), version);
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node main.mjs// Expected stdout: 6 2
TypeScript Reference