While using Object.entries() it returns correct values types, but keys are as string[] which is not correct. I want to force TS to be aware of my keys. I tried to use as const on object but this did nothing.
Is it possible to assert type in this case?
const demo = {
a: 'TEST',
b: 222,
} as const
Object.entries(demo)
.forEach(([key, value]) => { // key is string and not "a" | "b"
console.log([key, value])
})
// reproduce same types as above
Object.entries(demo)
.forEach(([key, value]: [string, typeof demo[keyof typeof demo]]) => {
console.log([key, value])
})
// now trying to change string to actual keys, error :(
Object.entries(demo)
.forEach(([key, value]: [keyof typeof demo, typeof demo[keyof typeof demo]]) => {
console.log([key, value])
})
// so instead trying to force somehow type assertion
Object.entries(demo)
.forEach(([key as keyof typeof demo, value]) => { // how to make assertion???
console.log([key, value])
})