I have very simple typescript code, just to iterate over an array of numbers:
const myTable = {};
// data is typed to 'number[]'
const data = [1,2,3,4,5,6,7,8,9,10];
// item is typed to 'string'
for(const item in data) {
// compiler error due to item is string type
myTable[item%2] = true;
}
Why typescript type item
to string
instead of number
when it knows data
is number[]
?