I would like to ask why the compiler doesn't let me use the callback function to forEach
although as far as I know "a" | "b" | "c" | "d"
is a subtype of string
. Please check out the error message after the code block. I guess I could solve this problem with a Record
type, correct? Thanks!
const obj = { a: 10, b: 20, c: 30, d: 40 }
type O = typeof obj
Object.keys(obj).forEach((value: keyof O) => {
console.log(value)
})
error TS2345: Argument of type '(value: "a" | "b" | "c" | "d") => void' is not assignable to parameter of type '(value: string, index: number, array: string[]) => void'.
Types of parameters 'value' and 'value' are incompatible.
Type 'string' is not assignable to type '"a" | "b" | "c" | "d"'.