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
// String enum
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
// Numeric enums
enum Direction {
Up = 1,
Down,
Left,
Right,
}
xxxxxxxxxx
enum Weekday {
MONDAY = 'mon',
TUESDAY = 'tue',
WEDNESDAY = 'wed'
}
type WeekdayType = `${Weekday}`;
xxxxxxxxxx
enum Provider {"google", "linkedin", "local"}
interface User {
authprovider: keyof typeof Provider
}
xxxxxxxxxx
//All of the following members are auto-incremented from that point on.
//In other words, Direction.Up has the value 1, Down has 2, Left has 3, and Right has 4.
enum Direction {
Up = 1, //If Up is not set to 1 will be 0 by default!
Down, //Down = 2
Left, //Left = 3
Right, //Right = 4
}
//String enum
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
xxxxxxxxxx
enum UserResponse {
No = 0,
Yes = 1,
}
function respond(recipient: string, message: UserResponse): void {
console.log(recipient, message); // "Princess Caroline", 1
}
respond("Princess Caroline", UserResponse.Yes);
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 = {}));