Why the type of object key is not properly picked by typescript?
const k: Record<'a' | 'b', number> = { a: 5, b: 6 };
Object.entries(k).forEach(([key, value]) => {
// key: string
// value: number
});
I was expecting the types to be
key: 'a' | 'b'
value: number
Because we are saying Record<'a' | 'b', number>
and not Record<string, number>
.
Also how can I fix this in a safe way with out using any
?
There is a possible duplicate here. But that question is not following the minimal-reproducible-example - and hence the answers are too. I felt like that thread is taking too much energy to understand because it is not generalized.