0

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
       }, {}),
}
3
  • 2
    Try giving acc and el better types. Commented Mar 15, 2021 at 19:05
  • Where is currentItem being declared? (acc: IItem[], el: IItem) might work for the arrow function but have you tried it without the type declaration? Commented Mar 15, 2021 at 19:05
  • It ditnt help unfornunatlly Commented Mar 15, 2021 at 19:21

1 Answer 1

1

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
    }, {});
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.