我似乎无法弄清楚如何在这里正确输入索引签名。我有一个枚举,需要遍历它以将一些 JSX 放在屏幕上。我可以猜到它在告诉我什么,但我无法在我的代码中解决它。这两种Category[el]
说法都有问题。
export enum Category {
All = 'ALL',
Employee = 'EMPLOYEE',
Gear = 'GEAR',
Room = 'ROOM',
Other = 'OTHER',
}
我渲染一些 JSX 的简化函数是:
const renderCategories = (): ReactElement | ReactElement[] => {
return Object.keys(Category).map(el => {
return (
<Option key={el} value={Category[el]}>
<span>{` (${someOtherData.filter((e) => e.type === Category[el].length})`}</span>
</Option>
);
});
};
TS告诉我:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof Category'.
No index signature with a parameter of type 'string' was found on type 'typeof Category'.