枚举
数字枚举
e.g. 红绿蓝 Red = 0,Green = 1,Blue = 2
// 默认从 0 开始,并递增enum Color { red, green, blue}console.log(Color.red) // 0console.log(Color.green) // 1console.log(Color.blue) // 2增长枚举
// 起始是 2,其余递增enum Color { red = 2, green, blue}console.log(Color.red) // 2console.log(Color.green) // 3console.log(Color.blue) // 4定义枚举
enum Color { red = 2, green = 4, blue = 6}console.log(Color.red) // 2console.log(Color.green) // 4console.log(Color.blue) // 6字符串枚举
enum Color { red = 'red', green, // 报错 枚举成员必须具有初始化表达式 blue = 'blue'}console.log(Color.red)console.log(Color.green)console.log(Color.blue)异构枚举
枚举可以混合类型
enum Flag { yes = 1, no = 'no'}接口枚举
enum Color { red = 'red', green = 'green', blue = 'blue'}
interface A { red: Color.red}
const obj: A = { red: Color.red}const 枚举
不能包含计算成员
const enum Person { name = 'Olu', age = 18, aa = 'qwe'.length // 报错 含字符串值成员的枚举中不允许使用计算值}
let otherName: string = 'Olu'
if (otherName === Person.name) { console.log('aha')}使用 const 声明枚举编译得到的 js 如下
var otherName = 'Olu';if (otherName === "Olu" /* Person.name */) { console.log('aha');}可以看出使用 const 声明枚举时,枚举被编译成了常量
enum Person { name = 'Olu', age = 18}
let otherName: string = 'Olu'
if (otherName === Person.name) { console.log('aha')}不使用 const 声明枚举编译得到的 js 如下
var Person;(function (Person) { Person["name"] = "Olu"; Person[Person["age"] = 18] = "age";})(Person || (Person = {}));var otherName = 'Olu';if (otherName === Person.name) { console.log('aha');}可以看出不使用 const 声明枚举时,枚举被编译成了对象
反向映射
包含正向映射(name => value)和反向映射(value => name)
string 类型不可以进行反向映射
enum Person { name}
let username = Person.nameconsole.log(username) // 0let item = Person[username]console.log(item) // name编译结果如下:
var Person;(function (Person) { Person[Person["name"] = 0] = "name";})(Person || (Person = {}));var username = Person.name;console.log(username);var item = Person[username];console.log(item);