Enum Member Name vs Value
What you'll learn: How to distinguish between the compile-time name of an enum member and its runtime value, and why this matters for type checking.
The Two Faces of an Enum Member
When you define an enum member, you're actually creating two distinct things:
- The name — the identifier you write in your code (compile-time)
- The value — what that member actually holds at runtime (string or number)
Think of it like a label on a jar: the label is the name ("Sugar"), but the value is what's actually inside the jar (maybe the string "SUGAR" or the number 0).
Why This Distinction Matters
TypeScript's type system works with names at compile time. When you use an enum member in your code, TypeScript sees the name as the type. But at runtime, JavaScript only knows about the value.
enum Status {
Pending = "PENDING",
Active = "ACTIVE"
}
const userStatus: Status = Status.Active;
// At compile time: TypeScript sees "Status.Active" (the name)
// At runtime: JavaScript stores "ACTIVE" (the value)
The name Active is what TypeScript uses for type checking. The value "ACTIVE" is what gets stored in memory and sent over networks.
Type Checks Use Names, Not Values
This means you cannot directly assign a raw value without TypeScript flagging it:
enum Role {
Admin = "ADMIN",
User = "USER"
}
const role: Role = Role.Admin; // ✓ Works - using the name
const role2: Role = "ADMIN"; // ✗ Error - using the value directly
Even though Role.Admin becomes "ADMIN" at runtime, TypeScript treats them as incompatible types during compilation.
Key Takeaway: Enum members have both a compile-time name (used for type checking) and a runtime value (what actually exists in JavaScript). TypeScript's type system operates on names, not values.