I'm puzzled by the fact that filter.formatter
(in the penultimate line) is showing as undefined even though I have already confirmed its existence:
type Filter = {
formatter?: {
index: number,
func: (value: string) => void
}
}
const filter: Filter = {
formatter: {
index: 1,
func: (v) => {
return `Label: ${v}`
}
}
}
const data = [['foo', 'bar']]
if (filter.formatter) {
data[filter.formatter.index].forEach(d => {
filter.formatter.func(d) // <-- `formatter` is possibly undefined
})
}
Update 1
@CRice pointed out in a comment that this issue doesn't arise in for
-loops. But I still can't figure out why?