TypeScript Advanced Declaration Authoring

Explicit public API declaration contracts

Explicit exported return contracts give the library a stable intentional public shape even when implementation details change. Generate declarations and validate them from a separate consumer. A correct runtime implementation alone does not establish that the consumer sees the intended type. 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 interface Result { /* public members */ } export function make(): Result;

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: 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)export interface Result { count: number }export function make(): Result { return { count: 7 }; }
// File: node_modules/demo/package.json (omit this label){  "name": "demo",  "main": "index.cjs",  "types": "index.d.cts"}
// File: main.cts (omit this label)import { make } from "demo";console.log(make().count);
// Run: tsc -p producer --pretty false// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node main.cjs// Expected stdout: 7
TypeScript Reference