如何在Typescript中反向映射枚举值

时间:2019-12-13 18:01:24

标签: typescript

我正在使用打字稿3.7 鉴于

enum InformationSection {
  Education = 'Education label',
  Resume = 'Resume label'
}

代码

InformationSection["Education"]

返回“教育标签”,这是有效的声明 但是如果我写

Object.keys(InformationSection).map(x => InformationSection[x])

然后出现以下错误:

Property 'Education label' does not exist on type 'typeof InformationSection'

这是怎么了?

1 个答案:

答案 0 :(得分:0)

我发现的唯一方法是欺骗打字稿,使其停止抱怨无效的索引类型

(InformationSection as any)[x]