我有接受Item的组件,ant我用接口来描述它,但在组件内部 我使用下一个构造(我使用另一个属性来获取 currentItem 字段)
modalComponentsData.reduce((acc: any, el: any) => {
acc[el.name] = currentItem[el.name]
return acc
}, {}),
然后我收到下一个警告
TS7053: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ name: string; id?: string | undefined; }'
interface IItem{
name: string,
id: string
}
export default function ModalGroupComponent({currentItem}: IItem{
....some logic
modalComponentsData.reduce((acc: any, el: any) => {
acc[el.name] = currentItem[el.name]
return acc
}, {}),
}
答案 0 :(得分:1)
试试这个:
interface IItem {
name: string;
id: string;
}
interface IItemDict {
[key: string]: IItem
}
interface IComponent {
currentItem: IItemDict;
}
interface IElement {
name: string;
}
export default function ModalGroupComponent({currentItem}: IComponent) {
// ....some logic
modalComponentsData.reduce((acc: IItemDict, el: IElement) => {
acc[el.name] = currentItem[el.name]
return acc
}, {});
}