Lesson 4 of 14 · 20m
Enums emit code your runtime may refuse
Why enum is non-erasable syntax Node won't run, what const enum breaks under transpile-only builds, and the as-const replacement.
Open any TypeScript file containing enum Direction { Up, Down } and look at the emit: the compiler generates a JavaScript object — an IIFE building Direction with both Direction.Up = 0 and Direction[0] = "Up". An enum is not a compile-time-only construct. It's runtime code. That single fact explains every modern problem with enums, including the newest one: Node.js flatly refuses to run them.
What an enum actually is
Numeric enums emit an object with reverse mapping — Direction.Up === 0 and Direction[0] === "Up". Iterating Object.keys(Direction) hands you both names and numbers, which has surprised everyone who built a dropdown from one. String enums emit a plain object without reverse mapping. const enum skips the object entirely: the compiler inlines each member's value at every use site, so Direction.Up becomes 0 in the emit — fast, and invisible to anything that processes files one at a time.
That invisibility is the first crack. esbuild, swc, Babel, and Node's own type stripper are transpile-only pipelines: they erase types per file with no cross-file type information. A const enum value lives in another file's type information, so a transpile-only tool literally cannot know what to inline. This is why isolatedModules treats const enum as a hazard and why tsconfig docs push preserveConstEnums only into narrow corners.
The second crack: type stripping
Node.js has run .ts files natively since v22.18/v23.6 by stripping types — replacing annotations with whitespace and executing the rest, no codegen. It became stable in v24.12/v25.2, and v26 removed the --experimental-transform-types escape hatch. The rule: only erasable syntax is allowed. The banned list is short — enum, namespace/module with runtime code, parameter properties in constructors, import =, and <T>-style casts. TypeScript 5.8 added erasableSyntaxOnly specifically to enforce this subset at compile time.
So in 2026, enum is the only "normal" TypeScript feature that fails on node worker.ts. Not deprecated — rejected by the runtime.
The replacement everyone actually uses
const Direction = { Up: "up", Down: "down" } as const;
type Direction = (typeof Direction)[keyof typeof Direction]; // "up" | "down"An as const object gives you a runtime value (names and values, your choice of mapping) plus a derived union type — keyof typeof for the keys, [keyof typeof] for the values. Erasable, iterable in the direction you chose, and no reverse-mapping ghosts. When you don't need a runtime object at all, the union alone is cheaper still:
type Direction = "up" | "down";That's the whole feature — a set of allowed strings the compiler checks, nothing emitted. Unions of literals narrow through === and switch exactly like enum members did, and they compose with discriminated unions cleanly ({ dir: "up" } | { dir: "down" }). Enum members work as discriminants too — what they can't do is exist without emitting a runtime object.
When an enum is still defensible
You control the whole build (a bundler, or tsc itself doing full emit), you never ship through a transpile-only path, and you want a single nameable bag of constants — e.g. an HttpStatus table. A plain enum there is legal and idiomatic. const enum, still skip: it silently breaks every transpile-only consumer, and in a published library that's your users, not you. The default answer for new code is a literal union or as const object; reach for enum when you've named a reason.
Try this
Take an enum in your codebase, rewrite it as const X = {...} as const plus type X = (typeof X)[keyof typeof X], and run tsc. Then run the file under node directly — before and after — to watch the erasable-syntax rule fire and clear.
Pitfalls
enum E { A }whereAimplicitly starts at 0 meansif (e)is falsy for the first member — truthiness bugs hide in numeric enums.declare const enumin.d.tsfiles promises values that must exist at runtime; mismatches are silent because nothing is emitted to check against.keyof typeof MyEnumgives the names,MyEnumthe member type — they differ, and mixing them produces odd assignability errors.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Building a Modern App With React, Vite, and Tailwind CSS: The Complete 2025 Guide
React for the UI, Vite for the build tooling, Tailwind for the styles. This is the stack most new frontend projects start with in 2025. This guide wires all three together from scratch and builds a real application structure you can actually start a production project from.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
What is React? The JavaScript Library That Changed Frontend Development
React is a JavaScript library for building user interfaces. Facebook built it in 2013, open-sourced it, and the entire frontend ecosystem reorganized itself around it. Here is how the component model works, why it took over, and what you need to know before writing a single line.
Angular 18: Deferred Views & Built-in Control Flow
The newest release of Angular – which is Angular 18 – is planned to be released on May 22nd, 2024. This is Google's super popular TypeScript based webapp framework that allows developers to create amazing web applications. The features, changes, and goodies of this release are deferrable views…