Enums and const object alternatives
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Declare numeric and string enums and read their emitted JavaScript.
- Explain why const objects with as const are often preferred.
- Use a union of literals where an enum would be tempting.
Overview
#Numeric enums
enum Direction { Up, Down, Left, Right }
Members default to 0, 1, 2, 3. The emitted JavaScript contains a real object with reverse mappings, so numeric enums add runtime code and, before TypeScript 5.0, accepted any number.
String enums
enum Status { Idle = "idle", Done = "done" }
String enums are clearer in logs and have no reverse mapping, but they still emit an object.
const enums
const enum is inlined at each use site and emits nothing. It breaks under isolatedModules and single-file transpilers, so many codebases avoid it.
The const object pattern
const Status = { Idle: "idle", Done: "done" } as const;
type Status = (typeof Status)[keyof typeof Status]; // "idle" | "done"
This gives a runtime object and a literal union type, tree-shakes cleanly, and needs no special compiler flag. The type and value share a name because TypeScript keeps them in separate namespaces.
Choosing
Prefer a union of string literals for a closed set of values that never needs runtime iteration. Use a const object when you also need the values at runtime. Reach for an enum only when a project convention already uses them.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What does this print at runtime? ```ts enum Direction { Up, Down } console.log(Direction.Up, Direction[0]); ```
Exercise 2Implement
Given `const Status = { Idle: "idle", Done: "done" } as const;`, derive `type StatusValue` and implement `isDone(value: StatusValue): boolean`.
starter.typescriptexport const Status = { Idle: "idle", Done: "done" } as const; export type StatusValue = (typeof Status)[keyof typeof Status]; export function isDone(value: StatusValue): boolean { // your code }Reveal the reference answer
reference.typescriptexport const Status = { Idle: "idle", Done: "done" } as const; export type StatusValue = (typeof Status)[keyof typeof Status]; export function isDone(value: StatusValue): boolean { return value === Status.Done; }Authored expected output: Compile-time: StatusValue is "idle" | "done". Runtime: isDone("done") is true, isDone("idle") is false.
keyof typeof reads the object keys and the indexed access turns them into a literal union.
Exercise 3Debug
This numeric enum emits a runtime object. Rewrite it as a const object with a derived union so the accepted values are an explicit literal union and no enum object is emitted. ```ts enum Level { Low, High } function setLevel(level: Level) {} setLevel(Level.Low); ```
starter.typescriptenum Level { Low, High } function setLevel(level: Level) {} setLevel(Level.Low);Reveal the reference answer
reference.typescriptconst Level = { Low: "low", High: "high" } as const; type Level = (typeof Level)[keyof typeof Level]; function setLevel(level: Level) {} setLevel(Level.Low);Authored expected output: Compile-time: with type Level = "low" | "high", only the two literals are accepted and no enum object is emitted; setLevel(99) fails to compile.
The union of literals keeps the accepted values explicit and tree-shakeable. The numeric enum emitted a runtime object and, before TypeScript 5.0, accepted any number.
Common misconceptions
#- Enums are erased like interfaces.
- A const enum always emits a runtime object.
- A string enum accepts arbitrary strings.
Follow-up practice
#- typescript-classes-implements
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.