xxxxxxxxxx
export enum MESSAGE_TYPE {
INFO = 1,
SUCCESS = 2,
WARNING = 3,
ERROR = 4,
};
var type = 3;
if (type in MESSAGE_TYPE) {
}
xxxxxxxxxx
enum Weekday {
MONDAY = 'mon',
TUESDAY = 'tue',
WEDNESDAY = 'wed'
}
type WeekdayType = `${Weekday}`;
xxxxxxxxxx
enum Sides {LEFT, RIGHT};
Sides.LEFT; // 0
Sides.RIGHT; // 1
const typeFromEnum: Sides.LEFT = Sides.LEFT; // Enums become types!
console.log(Sides); // { '0': 'LEFT', '1': 'RIGHT', LEFT: 0, RIGHT: 1 }
type leftOrRight = keyof typeof Sides; // 'LEFT' | 'RIGHT'
let sideName: string = Sides[0]; // 'LEFT' reverse mapping
enum EnumWithString {
X = "XX",
Y = "YY",
};
console.log(EnumWithString); // { X: 'XX', Y: 'YY' } no reverse mapping
xxxxxxxxxx
var TrafficLight;
(function (TrafficLight) {
TrafficLight[TrafficLight["RED"] = 0] = "RED";
TrafficLight[TrafficLight["YELLOW"] = 1] = "YELLOW";
TrafficLight[TrafficLight["GREEN"] = 2] = "GREEN";
})(TrafficLight || (TrafficLight = {}));
xxxxxxxxxx
enum Direction {
Up = 1,
Down,
Left,
Right,
}
We’ll first start off with numeric enums, which are probably more familiar if you’re coming from other languages. An enum can be defined using the enum keyword.
If we wanted, we could leave off the initializers entirely:
enum Direction {
Up,
Down,
Left,
Right,
}
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
xxxxxxxxxx
enum EMoney {
gopay = 'gopay',
dana = 'dana',
ovo = 'ovo'
}
enum Bank {
bca = 'bca',
mandiri = 'mandiri',
bri = 'bri'
}
interface OnlineShop<T> {
payment: T
}
const payment: OnlineShop<Bank> = {
payment: Bank.bca
}
xxxxxxxxxx
enum Number {
One,
Two
}
const numberOne: Number = "One" as Number;
const otherNumberOne: Number = Number.One;
const stringOne = Number[Number.One];