1enum AnEnum {
2 One = 1,
3 Two = 2
4}
5let stringOne = AnEnum[1]; // "One"
6let stringTwo = AnEnum[AnEnum.Two]; // "Two"
1const daysEnum = Object.freeze({
2 monday: 0,
3 tuesday: 1,
4 wednesday: 2,
5 thursday: 3,
6 friday: 4,
7 saturday: 5,
8 sunday: 6
9});
1const seasons = {
2 SUMMER: {
3 BEGINNING: "summer.beginning",
4 ENDING: "summer.ending"
5 },
6 WINTER: "winter",
7 SPRING: "spring",
8 AUTUMN: "autumn"
9};
10
11let season = seasons.SUMMER.BEGINNING;
12
13if (!season) {
14 throw new Error("Season is not defined");
15}
16
17switch (season) {
18 case seasons.SUMMER.BEGINNING:
19 // Do something for summer beginning
20 case seasons.SUMMER.ENDING:
21 // Do something for summer ending
22 case seasons.SUMMER:
23 // This will work if season = seasons.SUMMER
24 // Do something for summer (generic)
25 case seasons.WINTER:
26 //Do something for winter
27 case seasons.SPRING:
28 //Do something for spring
29 case seasons.AUTUMN:
30 //Do something for autumn
31}
1const DaysEnum = {"monday":1, "tuesday":2, "wednesday":3, ...}
2Object.freeze(DaysEnum)
3
1enum Response {
2 No = 0,
3 Yes = 1,
4}
5
6function respond(recipient: string, message: Response): void {
7 // ...
8}
9
10respond("Princess Caroline", Response.Yes)
11