typescript 常用枚举例子
时间: 2023-08-03 18:35:14 浏览: 104
在 TypeScript 中,枚举(Enum)是一种可枚举类型,它是一种具有有限可能值的数据类型。下面是一些 TypeScript 常用枚举的例子:
1. 方向枚举
```
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT"
}
```
2. 星期枚举
```
enum Weekday {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
```
3. 月份枚举
```
enum Month {
January = 1,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
}
```
4. 用户角色枚举
```
enum UserRole {
Admin,
User,
Guest
}
```
这些都是 TypeScript 中常用的枚举的例子。枚举可以用来限制变量的取值范围,让代码更加清晰易懂。
阅读全文