TypeScript Advanced Declaration Authoring

Package declaration entry points

A package can locate declarations separately from runtime code using its types entry point. The fixture resolves both paths from a bare import without aliasing producer source. A consumer that invokes an undeclared method is rejected. When exports controls resolution, ensure the matching declaration condition is present instead of relying solely on types. 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
{"main":"dist/index.cjs","types":"types/api.d.cts"}

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": "dist/index.cjs",  "types": "types/api.d.cts"}
// File: node_modules/demo/types/api.d.cts (omit this label)export function read(): number;
// File: node_modules/demo/dist/index.cjs (omit this label)exports.read = () => 6;
// File: main.cts (omit this label)import { read } from "demo";console.log(read());
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node main.cjs// Expected stdout: 6
TypeScript Reference