const expected_keys: String[] = [
"q",
"w",
"e",
"r",
"t",
"y"
];
export function lint(data: any): object {
var working_keys: String[] = [];
var linted_data: any = {};
for (let key in data) {
console.log(key); // Print 1
if (expected_keys.includes(key)) {
working_keys.push(key);
}
}
console.log(working_keys); // Print 2
for (let key in working_keys) {
console.log(key); // Print 3
linted_data[working_keys[key]] = data[working_keys[key]];
}
return linted_data;
}
On the first print, it looks like the following:
q
w
e
r
t
y
On the second print, it is also qwerty. However, for the third print, i get indexes 0-5 instead of qwerty. Why is this? Both of these arrays have the same type of String[]. Does it have something to do with push()?
Take data to be the following:
{
"q": "info",
"w": "info",
"e": "info",
"r": "info",
"t": "info",
"y": "info",
}
And how come I get the following error but the typescript still compiles and runs as expected?
Type 'String' cannot be used as an index type.
46 linted_data[working_keys[key]] = data[working_keys[key]];
string, notString. On mobile so I can't address the main question.data? You have it typed asanyand haven't included it so this doesn't constitute a minimal reproducible example. But it's obvious thatdatais some object type, and not an array. Andworking_keysis an array. The keys of an array are numeric. So you're iterating the keys of different things and they are different; not sure why you expect them to be the same.