November 2019
Beginner
804 pages
20h 1m
English
In TypeScript, it is also possible to define string enums. The following is an example:
enum TShirtType {
CrewNeck = "Crew Neck", // must be initialized with a constant
VNeck = "V Neck",
Henley = "Henley",
Polo = "Polo",
SpecialPolo = Polo, // may be initialized with another entry
ScoopNeck = "Scoop Neck"
}
let myTShirtType = TShirtType.CrewNeck;
console.log("My T-Shirt type: ", myTShirtType);
And again, the corresponding output, is as follows:
$ node ts-enums-02-string-enum.js My T-Shirt type: Crew Neck
String enums do not have the same numerical index value as numerical enums. The constants associated with the enum entry names are the values.
One direct benefit is that you can serialize/persist these values without worrying ...
Read now
Unlock full access