Here is the code snippet that I am working with:
dispatchEvent(name: TableEventName, params: any): any {
const event =
typeof this.options?.event?.[name] === 'function' ? this.options.event[name] : () => {}
return event(params)
}
When I compile it, I get the error message:
Cannot invoke an object which is possibly 'undefined'. return event(params)
I have gone through and checked my conditionals, so why is this error occurring?
Below is the full code for reference:
type TableEventName = 'onStateChange' | 'onSort' | 'onPageChange' | 'onSelection'
interface TableOptions {
event?: {
onStateChange?: (event: any) => void
onSort?: (event: any) => void
onPageChange?: (event: any) => void
onSelection?: (event: any) => void
}
}
let options: TableOptions = {
event: {
onSort: () => { }
}
}
let dispatcher = function (name: TableEventName, params: any): any {
const event =
typeof options?.event?.[name] === 'function' ? options.event[name] : () => {}
return event(params)
}