TypeScript Compiler Options
preserveConstEnums — TypeScript compiler option
Retains a runtime enum object for const enums while ordinary references may still be inlined. With isolatedModules, preservation is required and references may remain runtime property accesses. Avoid exporting ambient const enums across package boundaries without considering consumer compilation and version skew. Default: false in ordinary non-isolated compilation. Create the named files in a clean local project. Run tsc -p tsconfig.json. Expected output files: dist/example.js. Run node dist/example.js; expected stdout: '2'. Tested using TypeScript 5.8.3 and Node.js 22.16.0; local project instructions, not a single-file editor.
Formal Syntax & Structure
typescript syntaxtypescript
{ "compilerOptions": { "preserveConstEnums": true }}Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| preserveConstEnums | boolean | Set in compilerOptions; default: false in ordinary non-isolated compilation. |
Working Example
Example Snippettypescript
// File: tsconfig.json (omit this label){ "compilerOptions": { "target": "ES2022", "module": "CommonJS", "strict": true, "types": [], "noEmitOnError": true, "outDir": "dist", "preserveConstEnums": true }, "files": [ "example.ts" ]}// File: example.tsexport const enum State { Ready = 2 }console.log(State.Ready);