TypeScript Advanced Declaration Authoring

Internal type leakage in public declarations

A nonexported type can be needed to describe an exported function. Keeping its declaration allows consumers to use the returned shape. stripInternal can remove the needed declaration while retaining the public reference, yielding a broken declaration package. The negative consumer checks the actual generated file with skipLibCheck off. Prefer a deliberately public result contract rather than hiding a required 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
/** @internal */ interface Hidden { ... } export function read(): Hidden;

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,    "stripInternal": false  },  "files": [    "index.cts"  ]}
// File: producer/index.cts (omit this label)/** @internal */interface Hidden { count: number }export function read(): Hidden { return { count: 2 }; }
// 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 { read } from "demo";console.log(read().count);
// Run: tsc -p producer --pretty false// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node main.cjs// Expected stdout: 2
TypeScript Reference