Inspecting emitted JavaScript before replacing TypeScript enums with literal unions
A developer tested how TypeScript enums, literal unions, and as const objects behave at runtime, using TypeScript 7.0.2 and Node.js 24.15.0. Regular numeric enums emit JavaScript objects with reverse mappings, while string enums emit only forward mappings, and literal unions disappear entirely after compilation. The experiment showed that a string matching an enum member's value is still rejected when assigned directly to a string enum type, producing error TS2322. Under isolatedModules set to false, a const enum was inlined into the output, whereas with isolatedModules set to true the object access remained, and the erasableSyntaxOnly option rejected both regular and const enums. The author notes that these results apply to same-file definitions and do not cover ambient declarations or mismatched library versions.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in