【发布时间】:2020-12-22 00:26:46
【问题描述】:
我似乎不知道如何在此处正确键入索引签名。我有一个枚举,需要遍历它以将一些 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'.
【问题讨论】:
标签: reactjs typescript index-signature