-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make EventListener covariant #798
Comments
The solution evtSource.addEventListener("messageEvt", ((e: MessageEvent) => {}) as EventListener); Doesnt work for me either, when I use it with |
@exapsy React.MouseEvent is not meant to be used with EventSource. I think your issue is a different one. |
I have encountered this issue using EventSource with custom message types as in this tutorial: https://javascript.info/server-sent-events#event-types I am working around this by redefining the types of --- lib.dom.d.ts 2020-06-23 15:11:30.000000000 -0600
+++ patched.d.ts 2020-06-23 15:11:12.000000000 -0600
@@ -21,8 +21,9 @@
readonly CLOSED: number;
readonly CONNECTING: number;
readonly OPEN: number;
+
addEventListener<K extends keyof EventSourceEventMap>(type: K, listener: (this: EventSource, ev: EventSourceEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
- addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
+ addEventListener(type: string,listener: (this: EventSource, ev: MessageEvent) => any, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof EventSourceEventMap>(type: K, listener: (this: EventSource, ev: EventSourceEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
- removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
+ removeEventListener(type: string, listener: MessageEvent, options?: boolean | EventListenerOptions): void;
} I think in the case of messages from an EventSource it makes sense that any event other than |
Hello,
I opened microsoft/TypeScript#35211 without realizing that the
lib.dom.d.ts
file was a generated file. I'm copying the PR description here to see if people think it is a good idea. If so, I'd appreciate some direction to get this implemented given I'm not familiar with how these types are being generated after looking through the source code.Original Typescript PR 35211 Description
Currently, the
EventListener
interface is invariant. As a result, functions that pass a subtype ofEvent
fail to compile (e.g. see theCustomEvent
andMessageEvent
examples below).CustomEvent handler
MessageEvent handler
As a result, it seems like a lot of people resort to using the
as EventListener
type assertion.Instead of having users resort to the
as
operator, I'm proposing we introduce a generic type variable that extendsEvent
to theEventListener
interface. We could then pass the appropriate sub-type to theaddEventHandler
functions for the respective objects (e.g.EventSource
below).The net result would be to allow users to simply do:
11/19/19: Note that the PR currently only implements the changes for the
EventSource
interface. I'd like to get some feedback before porting the change to every other object.The text was updated successfully, but these errors were encountered: