为什么 Enum[Enum.member] 在 codepen.io typescript playground 未定义?
Why Enum[Enum.member] is undefined at codepen.io typescript playground?
我只是计划在 codepen.io 使用 TypeScript playground 检查一些类型设置
并出人意料地面临以下问题
enum Order {
Asc = 'asc',
Desc = 'desc'
}
console.log(Order[Order.Asc]); // undefined in codepen.io playground
当然,当使用 webpack 等编译时,它会导致 Asc
引用自https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
...an enum is compiled into an object that stores both forward (name ->
value) and reverse (value -> name) mappings.
[...]
Keep in mind that string enum members do not get a reverse mapping
generated at all.
这是反向映射对数字的处理方式:
enum Order {
Asc,
Desc
}
Order.Asc // 0
Order[Order.Asc] // "Asc"
我只是计划在 codepen.io 使用 TypeScript playground 检查一些类型设置 并出人意料地面临以下问题
enum Order {
Asc = 'asc',
Desc = 'desc'
}
console.log(Order[Order.Asc]); // undefined in codepen.io playground
当然,当使用 webpack 等编译时,它会导致 Asc
引用自https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
...an enum is compiled into an object that stores both forward (name -> value) and reverse (value -> name) mappings. [...]
Keep in mind that string enum members do not get a reverse mapping generated at all.
这是反向映射对数字的处理方式:
enum Order {
Asc,
Desc
}
Order.Asc // 0
Order[Order.Asc] // "Asc"