This is now part of the language. See TypeScriptLang.org > Basic Types > enum for the documentation on this. An excerpt from the documentation on how to use these enums:
enum Color {Red, Green, Blue};
var c: Color = Color.Green;
Or with manual backing numbers:
enum Color {Red = 1, Green = 2, Blue = 4};
var c: Color = Color.Green;
You can also go back to the enum name by using for example Color[2]
.
Here's an example of how this all goes together:
module myModule {
export enum Color {Red, Green, Blue};
export class MyClass {
myColor: Color;
constructor() {
console.log(this.myColor);
this.myColor = Color.Blue;
console.log(this.myColor);
console.log(Color[this.myColor]);
}
}
}
var foo = new myModule.MyClass();
This will log:
undefined 2 Blue
Because, at the time of writing this, the Typescript Playground will generate this code:
var myModule;
(function (myModule) {
(function (Color) {
Color[Color["Red"] = 0] = "Red";
Color[Color["Green"] = 1] = "Green";
Color[Color["Blue"] = 2] = "Blue";
})(myModule.Color || (myModule.Color = {}));
var Color = myModule.Color;
;
var MyClass = (function () {
function MyClass() {
console.log(this.myColor);
this.myColor = Color.Blue;
console.log(this.myColor);
console.log(Color[this.myColor]);
}
return MyClass;
})();
myModule.MyClass = MyClass;
})(myModule || (myModule = {}));
var foo = new myModule.MyClass();