Skip to content

OutputNot attempted

Numeric enums have a reverse mapping

enum Direction { Up = 1, Down, Left, Right }
enum Role { Admin = 'admin', Reader = 'read_only' }
console.log(Direction.Down, Direction[2], Direction[4]);
console.log(Object.keys(Direction));
console.log(Role.Admin, (Role as any)['admin']);

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

Numeric enums auto-increment from the first value AND compile to an object with a reverse mapping (value → name), which is why Object.keys shows the numbers too. String enums have no reverse mapping. Enums are one of the few TS features that emit runtime code.