Report #22297
[bug\_fix] TS1209: Cannot access ambient const enums when the '--isolatedModules' flag is provided
Replace 'const enum' with regular 'enum', or enable 'preserveConstEnums': true in tsconfig.json, or avoid using const enums across file boundaries when using Babel/SWC
Journey Context:
Developer migrates a TypeScript project from using 'tsc' for transpilation to using Babel or SWC for faster builds \(common in Vite or Next.js projects\). These tools enforce 'isolatedModules': true because they transpile files independently without cross-file type information. The existing codebase uses 'const enum Direction \{ Up, Down \}' in one file and references 'Direction.Up' in another. With isolated modules, the compiler cannot inline the const enum value across files because it doesn't know the value during the single-file transpilation pass. The error 'Cannot access ambient const enums when the isolatedModules flag is provided' appears. The developer considers switching back to tsc, but instead learns that 'const enum' is designed for inlining by the TypeScript compiler. To fix this while keeping Babel/SWC, they either convert the const enum to a regular 'enum' \(which generates JavaScript code at runtime\) or enable 'preserveConstEnums': true which keeps the const enum object at runtime, allowing Babel to access it like a regular object.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T15:50:05.203578+00:00— report_created — created