Numeric enums

enum Color1 {Red, Green, Blue}
const c1: Color1 = Color1.Green;

console.log(c1); // 1

enum Color2 {Red = 1, Green, Blue}
const c2: Color2 = Color2.Green;

console.log(c2); // 2

enum Color3 {Red = 1, Green = 2, Blue = 4}
const c3: Color3 = Color3.Green;

console.log(c3); // 3

enum Color4 {Red = 1, Green, Blue}
const colorName: string = Color4[2];

console.log(colorName); // Green

String enums

enum Direction {
    Up = "UP",
    Down = "DOWN",
    Left = "LEFT",
    Right = "RIGHT",
}

const direction: Direction = Direction.Left;

console.log(direction); // 'LEFT'

Heterogeneous enums

근데 왜?

enum BooleanLikeHeterogeneousEnum {
    No = 0,
    Yes = "YES",
}

const what: BooleanLikeHeterogeneousEnum = BooleanLikeHeterogeneousEnum.Yes;

console.log(what); // 'YES'

Reverse mappings (Only Numeric)

enum EnumAB {
    A,
    B
}

const b = EnumAB.B;
const nameOfB: string = EnumAB[b];

console.log(b, nameOfB); // 1 'B'

const enums

enum Directions {
    Up,
    Down,
    Left,
    Right
}

const d: Directions = Directions.Left;

console.log(d);
console.log(Directions);
var Directions;
(function (Directions) {
    Directions[Directions["Up"] = 0] = "Up";
    Directions[Directions["Down"] = 1] = "Down";
    Directions[Directions["Left"] = 2] = "Left";
    Directions[Directions["Right"] = 3] = "Right";
})(Directions || (Directions = {}));
var d = Directions.Left;
console.log(d);
console.log(Directions);
const enum Directions {
    Up,
    Down,
    Left,
    Right
}

const d: Directions = Directions.Left;

console.log(d);
// console.log(Directions);
var d = 2 /* Left */;
console.log(d);
// console.log(Directions);