我的枚举定义如下:
export enum SwitchEnum{
On = 'ON',
Off = 'OFF',
}
我想从此枚举创建一个对象数组,因此我try 了以下操作:
export interface SelectProps {
id: number;
value: string | number | undefined;
label: string | undefined;
}
const listItems: SelectProps [] = Object.keys(SwitchEnum).map((key, index) => ({
id: index,
value: SwitchEnum[key],
label: key
}));
但Typescript抱怨此错误:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof SwitchEnum'.
No index signature with a parameter of type 'string' was found on type 'typeof SwitchEnum'
我如何解决这个问题?