I'd like to use an array to iterate through interface properties, however it's throwing a ts compile error on the forEach function:
interface A {
foo: string
bar: string
baz: string
}
function fn(obj: A) {
;["foo", "bar"].forEach( (prop: keyof A) => {
obj[prop] // do stuff
})
}
The error:
Argument of type '(prop: keyof A) => void' is not assignable to parameter of type '(value: string, index: number, array: string[]) => void'. Types of parameters 'prop' and 'value' are incompatible. Type 'string' is not assignable to type 'keyof A'
Something like this would work, but it feels redundant:
function fn(obj: A) {
const ar: (keyof A)[] = ["foo", "bar"]
ar.forEach( (prop: keyof A) => {
obj[prop] // do stuff
})
}