Reputation: 375
I'm using Typescript with strict compiler options which I really like. But when I have a for loop I get the following error a Element implicitly has an 'any' type because index expression is not of type 'number'
. I changed let i
to let i:number
shouldn't that fix it?
Here is the
Upvotes: 1
Views: 8842
Reputation: 81
This error has nothing to do with your i
variable.
event.touches
's type is TouchList, this is how it looks:
interface TouchList {
readonly length: number;
item(index: number): Touch | null;
[index: number]: Touch;
}
The index has to be a number yet Object.keys returns a string[]
and this is why you have a type mismatch.
You need to cast the string to number. You can do something as simple as Number(keys[i])
but there are at least two keys (length & item) that are not numbers so I recommend that you check that your casting returns an actual number first and only then push it to this.touches
Upvotes: 0
Reputation: 2295
You can cast the value of keys[i]
:
this.touches.push(event.touches[(keys[i] as number)])
Or if you expect it to be string then (keys[i] as string)
Upvotes: 1