Add a generic parameter to EventTarget for mapping events #43477
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
π Search Terms
EventTarget, addEventListener, removeEventListener
β Viability Checklist
β Suggestion
Currently we can extend
EventTarget
to hook any given class into the DOM events system:However, in the case of
EventTarget
, it has anaddEventListener
definition like so:Due to this, it seems impossible to strongly type the events of our class.
Remember how things like
window
work:This means we have strongly typed event names and types, giving us good intellisense when we do
window.addEventListener
.So my suggestion is that we do similar for
EventTarget
:We'd still want an overload which consumes
string
but that would just fall back toEvent
.Also, I'm unaware of any work around, so if there is already a known one, please do tell.
We also can't override this as a workaround, for example:
as this would be like doing:
You could possibly hack around it with interfaces:
The text was updated successfully, but these errors were encountered: