Rfa dada
Rfa dada

Reputation: 49

Typescript Element implicitly has an 'any' type because expression of type 'any' can't be used to index type

I have component that accept Item, ant I use interface to describe it, but inside component I use next construction (I use another property to get currentItem field)

modalComponentsData.reduce((acc: any, el: any) => {
         acc[el.name] = currentItem[el.name]
         return acc
       }, {}),

And I get next warning

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

Upvotes: 0

Views: 446

Answers (1)

Slavik Meltser
Slavik Meltser

Reputation: 10361

Try this:

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

Upvotes: 1

Related Questions