playground
const foo = {
a: 1,
b: 2,
c: 3
};
Object.keys(foo).forEach(key => {
foo[key]++; // error: expression of type 'string' can't be used to index ....
})
There must be multiple ways to solve it. I'm looking for a recommended way with least impact on readability. Thanks!
==========
Edit:
Bonus: would be great if I can still have autocompletion when I type foo. in my codebase.
This should work:
(Object.keys(foo) as (keyof typeof foo)[]).forEach(key => {
foo[key]++;
})
This isn't automatically done for you, because in theory it could be unsafe, but I played with various scenarios and couldn't produce a working demo with a runtime error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With