Typescript元素隐式具有一个';任何';类型,因为表达式的类型为';任何';can';不能用于索引类型

Typescript元素隐式具有一个';任何';类型,因为表达式的类型为';任何';can';不能用于索引类型,typescript,Typescript,我有一个接受项的组件,我使用接口来描述它,但在组件内部 我使用下一个构造(我使用另一个属性获取currentItem字段) 我得到下一个警告 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: st

我有一个接受项的组件,我使用接口来描述它,但在组件内部 我使用下一个构造(我使用另一个属性获取currentItem字段)

我得到下一个警告

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
       }, {}),
}
试试这个:

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
    }, {});
}

尝试给出更好的
acc
el
类型。声明
currentItem
的位置在哪里<代码>(acc:IItem[],el:IItem)可能适用于arrow函数,但您是否在没有类型声明的情况下尝试过它?它不太可能有帮助
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
    }, {});
}