BeCuriouzLearn something new every day
Enums, and Why Many Teams Skip Them
💡 Enums name a set of constants but leave runtime code behind, so a const object plus a union type is often the cleaner, fully-erasable alternative.
An enum gives a name to a set of related constants. The classic use is a fixed list of options:
enum Direction {
Up,
Down,
}
// Direction.Up === 0, Direction.Down === 1
let move: Direction = Direction.Up;By default TypeScript assigns numeric values (0, 1, 2…). That's a pitfall: Direction.Up is just 0, so any stray number sneaks in, and the value in your logs is a meaningless 0 instead of "Up". String enums (Up = "UP") fix the readability half but still generate real runtime code — unlike the types from earlier cards, an enum is not erased when TypeScript compiles to JavaScript.
That runtime footprint is why many teams prefer a const object plus a union type. You get the same autocomplete and the same narrow type, but it compiles to a plain object:
const Direction = {
Up: "UP",
Down: "DOWN",
} as const;
type Direction = typeof Direction[keyof typeof Direction];
// Direction = "UP" | "DOWN"
let move: Direction = Direction.Up;The as const freezes the values into literal types (recall literal types from earlier), and the union "UP" | "DOWN" is what actually flows through your code. It's transparent, tree-shakeable, and behaves like ordinary JavaScript — no special enum semantics to memorize.