Lesson 10 of 80
A named set of related constants — genuinely useful, but the one TypeScript feature that generates real runtime JavaScript, which is exactly why the community increasingly reaches for a plainer alternative instead.
11 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
For a numeric enum `enum Status { Pending, Active }`, what does `Status[0]` evaluate to at runtime?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
enum Status {
Pending, // 0
Active, // 1
Closed, // 2
}
function describe(s: Status) {
if (s === Status.Active) return "active";
}
Unlike almost every other TypeScript-only construct (interfaces, type aliases, generics), an enum isn't erased at compile time — it compiles down to a real JavaScript object at runtime (roughly { Pending: 0, Active: 1, ..., 0: "Pending", 1: "Active", ... } for a numeric enum). That reverse mapping and the extra generated code is unusual for TypeScript, and it's the root of most enum criticism.
const Status = {
Pending: "pending",
Active: "active",
Closed: "closed",
} as const;
type Status = (typeof Status)[keyof typeof Status]; // "pending" | "active" | "closed"
This "as-const object" pattern gives you the same named-constants experience with zero extra runtime code beyond the object itself — no reverse mapping, and the resulting type is a plain union of string literals, which plays more predictably with the rest of the type system than an enum's own distinct type does.
enum Status { Active, Paused, Done } — compiles to a real JS object at runtime, not erased like other types.
Status[0] gives you "Pending" back, code most people don't expect exists until they see it in the compiled output.enum — the runtime values are the actual string names, not sequential numbers that shift if you reorder members."Enum, or a union of string literals?" is a real, live debate in the TypeScript community, and being able to articulate the actual tradeoff (runtime cost and reverse mapping vs. named-group ergonomics) shows practical experience, not just textbook syntax knowledge.