Mastering TypeScript 3
上QQ阅读APP看书,第一时间看更新

String enums

A further variant of the enum type is what is known as a string enum, where the numeric values are replaced by strings, as follows:

enum DoorStateString { 
    Open = "open", 
    Closed = "closed", 
    Ajar = "ajar" 
} 
 
var openDoorString = DoorStateString.Open; 
console.log(`openDoorString = ${openDoorString}`); 

Here, we have an enum named DoorStateString, where each of the enum values are now of type string. The output of this code snipped would be as follows:

openDoorString = open

As expected, the TypeScript compiler is resolving the enum value of DoorStateString.Open to the string "open".