feat: add type parameter to EventTarget#1712
Conversation
This introduces a type parameter to `EventTarget` such that the
following is now possible:
```ts
interface CustomMap {
'test-event': CustomEvent<{x: number; y: number}>;
}
declare const customTarget: EventTarget<CustomMap>;
customTarget.addEventListener('test-event', (event) => {
// event is now `CustomEvent<{x: number; y: number}>`
event.detail.x;
event.detail.y;
});
```
Defaults to `T = any` to maintain existing behaviour otherwise.
|
Thanks for the PR! This section of the codebase is owned by @saschanaz - if they write a comment saying "LGTM" then it will be merged. |
|
This sounds nice to me and perhaps we can stop emitting duplicate addEventListeners and migrate to this method. @sandersn might want to check too. |
|
How is this different from #1624 ? My memory is that that (plus previous attempts) was too slow and also likely to cause breaks, but I can't remember if I finished testing the most recent attempt. |
|
@sandersn it seems the other PR is for making events generic such that if performance allows, we'd probably want both whats the best way for me to perf test this? |
This introduces a type parameter to
EventTargetsuch that the following is now possible:Defaults to
T = anyto maintain existing behaviour otherwise.Reviewer notes
i did two attempts at this:
EventTargetwhere it will then emit event handlers1 - overrides (this PR)
i don't like that i'm basically repeating the signatures of
emitEventHandlersmanually in the overrides JSON. though the result does seem to work well.maybe there's a cleaner way to do this?
2 - update the emitter
we could just change
emitEventHandlersto ultimately do this logic:which is then used to print the signatures.
i've tried this and it failed because it adds those signatures rather than replacing the existing ones.
any advice would be helpful of how to approach this
also if there's some fundamental reason this can never work, im of course happy to close the PR but would like to understand first