NOT LOOKING FOR A SOLUTION HERE, JUST NEED TO KNOW IF THIS IS A BUG IN TYPESCRIPT. THAT IS ALL.
Is this a bug in TypeScript? Should I report this bug? To my understanding this should work, unless I am doing something wrong here,
interface Something {
key1: string;
key2: number;
key3: boolean;
}
const someObject: Something = {
key1: '123',
key2: 123,
key3: false
}
for (const key in someObject) {
console.log(someObject[key]); // TS error
}
Note: I am not looking for a workaround, because this works for me. Just looking to know if I am doing something wrong or there is actually a bug in TS.
for (const [key, value] of Object.entries(someObject)) {
console.log(key, value);
}
or
for (const key in values) {
console.log(key, values[key as keyof DirectDepositDTO]);
}
I do understand that the type of key
is string
, but shouldn't it be keyof Something
?
P.S. The question is marked as a duplicate, but I am not asking for a solution here, I already know how to fix it. All I need to know if that is a bug in TypeScript. Because for (const key in someObject) {
means that the key
will be keyof Something
. I should not need to know re-define the types.