【发布时间】:2021-05-26 12:26:06
【问题描述】:
我正在尝试使用此代码将枚举结构从 [key:value] 的值作为字符串转换为 [value:key] 结构。
我的错误是
Element implicitly has an 'any' type because expression of type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | ... 31 more ... | "trimEnd"' can't be used to index type 'typeof Country'.
No index signature with a parameter of type 'number' was found on type 'typeof Country'
作为国家/地区的键
枚举
export enum Country {
UnitedStates = 'US',
Afghanistan = 'AF',
AlandIslands = 'AX',
}
代码
public countries = Object.keys(Country)
.slice(Object.keys(Country).length / 2)
.map(key => ({
label: key,
key: Country[key as keyof Country],
}));
当枚举值为 int 时,此代码有效。
【问题讨论】:
标签: angular typescript enumerable