This is due to the behavior of the --strictFunctionTypes
compiler flag added in TypeScript v2.6. A function of type (e: CustomEvent) => void
is no longer considered to be a valid instance of EventListener
, which takes an Event
parameter, not a CustomEvent
.
So one way to fix it is to turn off --strictFunctionTypes
.
Another way is to pass in a function that takes an Event
and then narrows to CustomEvent
via a type guard:
function isCustomEvent(event: Event): event is CustomEvent {
return 'detail' in event;
}
window.addEventListener('OnRewards', (e: Event) => {
if (!isCustomEvent(e))
throw new Error('not a custom event');
})
A third way is to use the other overload of addEventListener()
:
addEventListener<K extends keyof WindowEventMap>(type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, useCapture?: boolean): void;
If the type
parameter is the name of a known event type (K extends keyof WindowEventMap
) like "onclick"
, then the listener
function will expect its parameter to be of that narrowed event type (WindowEventMap[K]
). The problem is that "OnRewards"
is not a known event type... unless you use declaration merging to make it known:
interface WindowEventMap {
OnRewards: CustomEvent
}
Or, if you're inside a module (anything with export
in it), use global augmentation:
declare global {
interface WindowEventMap {
OnRewards: CustomEvent
}
}
Then use your code as before:
// no error!
window.addEventListener('OnRewards', (e: CustomEvent) => {
// my code here
})
So, those are your options. Which one you want to choose is up to you. Hope that helps; good luck!
CustomEvent
? Maybe try withe: Event
? – Explosion PillsCustomEvent
isvar event = new CustomEvent('OnRewards', { detail: data });
– Get Off My LawnEvent
then it doesn't know whatdetail
is inside of the function – Get Off My Lawn