TypeScript Advanced Declaration Authoring
Constructible library declarations
A class declaration describes both a constructible value and its instance members. A separate class implementation is required at runtime; private implementation details need not appear unless they affect type compatibility. The consumer constructs the exported class with new. 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
declare class Name { constructor(value: Type); method(): ReturnType; } export = Name;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": "./index.cjs", "types": "./index.d.cts"}
// File: node_modules/demo/index.d.cts (omit this label)declare class Meter { constructor(value: number); read(): number; }export = Meter;
// File: node_modules/demo/index.cjs (omit this label)module.exports = class Meter { constructor(value) { this.value = value; } read() { return this.value; } };
// File: main.cts (omit this label)import Meter = require("demo");console.log(new Meter(9).read());
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node main.cjs// Expected stdout: 9