Advertisement
Intermediate TypeScript, Enums

What is 'reverse mapping' in numeric enums, and why don't string enums support it?

Short answer

Numeric enums generate a two-way lookup object at runtime — you can go from name to value AND from value back to name (Direction[0] === 'Up').

Save TypeScript mock interview
Advertisement
AModel answer
Numeric enums generate a two-way lookup object at runtime — you can go from name to value AND from value back to name (Direction[0] === 'Up'). String enums only generate a one-way object, since having numeric values map back to keys would be ambiguous/meaningless for string values.
enum Direction { Up, Down }
console.log(Direction.Up);   // 0
console.log(Direction[0]);   // 'Up' -- reverse mapping, numeric enums only
Real-world example
Converting a stored numeric status code back into its readable name for logging or display purposes.
Likely follow-ups
  • Does this reverse mapping affect the size of the generated JavaScript output?
Related topics