W
W
wiincked2020-03-05 11:20:39
typescript
wiincked, 2020-03-05 11:20:39

How to remove the error Element implicitly has an 'any' type because expression of type 'string'?

Good afternoon, tell me how to remove this error

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'IItems'.
No index signature with a parameter of type 'string' was found on type 'IItems'

5e60b68ad1d85346119510.png

https://codesandbox.io/s/objective-blackwell-qj3il

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2020-03-05
@wiincked

Because TS doesn't know what exactly is in the variable id. He only knows that there is some string. And since it idcan be any string, the value ITEMS[0][id]can also be anything.
You can change your interface by specifying what values ​​id can take

interface IFilters {
    id: keyof IItems; // <-- "id" | "filter1" | "filter2"
    title: string;
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question