- Mastering TypeScript 3
- Nathan Rozentals
- 95字
- 2021-07-02 12:42:43
Const enums
A slight variant of the enum type is the const enum, which simply adds the keyword const before the enum definition, as follows:
const enum DoorStateConst { Open, Closed, Ajar } var constDoorOpen = DoorStateConst.Open; console.log(`constDoorOpen is : ${constDoorOpen}`);
const enums have been introduced largely for performance reasons. Let's take a quick look at the JavaScript that is generated from this DoorStateConst enum:
var constDoorOpen = 0 /* Open */;
Note how the compiler has simply resolved the DoorStateConst.Open enum to its internal value of 0, and removed the const enum definition entirely.