Reverse Mappings Create Confusion
You'll learn how numeric enums create bidirectional lookups that can lead to unexpected object structures and runtime behavior.
The Hidden Bidirectional Map
Unlike string enums, numeric enums create reverse mappings automatically. This means TypeScript generates both value-to-name and name-to-value lookups in the compiled JavaScript object.
When you declare a numeric enum, the resulting object contains twice as many properties as you defined — every name points to its value, and every value points back to its name.
This reverse mapping exists to let you look up an enum member's name from its numeric value at runtime. While occasionally useful, it creates a confusing object shape that catches developers off guard.
The Confusion in Practice
enum Status {
Pending = 0,
Active = 1,
Done = 2
}
// You expect: { Pending: 0, Active: 1, Done: 2 }
// You actually get:
// {
// Pending: 0, Active: 1, Done: 2,
// 0: "Pending", 1: "Active", 2: "Done"
// }
console.log(Status.Pending); // 0
console.log(Status[0]); // "Pending" (reverse lookup)
console.log(Object.keys(Status)); // ["0", "1", "2", "Pending", "Active", "Done"]
This creates problems when iterating over enum properties, serializing to JSON, or debugging — the object contains unexpected numeric string keys that aren't part of your mental model.
String enums avoid this entirely by not generating reverse mappings, keeping the object shape predictable.
Key Takeaway: Numeric enums create bidirectional mappings where values map back to names, doubling the number of properties and creating confusion when inspecting or iterating the enum object — use string enums to avoid this behavior.