const
keyword. Here are the examples of a regular enum:enum Direction {
Up,
Down,
Left,
Right
}
const
enum:const enum Light {
Red,
Green,
Blue
}
Direction
enum above, it will be transpiled to the following JavaScript code:var Direction;
(function (Direction) {
Direction[Direction["Up"] = 0] = "Up";
Direction[Direction["Down"] = 1] = "Down";
Direction[Direction["Left"] = 2] = "Left";
Direction[Direction["Right"] = 3] = "Right";
})(Direction || (Direction = {}));
Light
enum is not transpiled at all. You will see nothing if the enum is not used. console.log(Light.Red)
is compiled as
console.log(0 /* Red */)
.const
enum is generated at run time, it is not possible to loop over the const
enum values.Light
enum:// ERROR
for (let i in Light) {
console.log(i);
}
const
enums, you can use the preserveConstEnums
compiler flag.