A
A
Anon33632020-10-08 11:08:41
typescript
Anon3363, 2020-10-08 11:08:41

How to fix Property 'item' does not exist on type 'Record' error?

for(let item in classes){
    classes.item = 'page'+item
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aetae, 2020-10-08
@Anon3363

First: as YavaDev rightly pointed out : classes[item].
Secondly: only manually, at your own peril and risk:

for(let item in classes) {
    classes[item as keyof typeof classes] = 'page'+item
}
Typescript did not specifically add automatic typing of keys, leaving it simple string, because due to its structural nature it is impossible to say for sure whether the resulting object (or its prototype) has any other keys other than those specified in its type, and, accordingly, you can not be sure that nothing will break.

L
Loli E1ON, 2020-10-08
@E1ON

You need to add the item property to the Record type

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question