-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Improve type inference for the listener callback #19
Conversation
the difference is that |
what happens if you run |
Well, my best guess is that the type inference for generic types is done only when not specifying it at all, but not when specifying part of it and not next ones. Btw, with the existing code, |
For the record, my own usage is that I write JavaScript code, not TypeScript code. But I still rely on typescript type definitions for type checking ( |
I want to merge this but is it possible to achieve the same effect without changing the generic? For 2 reasons:
|
Well, I don't think it is possible without changing it. The change of generic is precisely what allows the event type argument to specify the event class in the generic. |
Can you change it so the generic takes 2 strings? At least it's both consistent and shorter Before<HTMLTextAreaElement, KeyboardEvent> This PR<HTMLTextAreaElement, 'keydown'> Suggested<'textarea', 'keydown'> |
Instead of using a generic Event type for the argument, the map of event types to the corresponding Event object types is used by the inference. This is a BC break for projects specifying the generic types explicitly, as it changes the generic signature. For project relying on type inference, the inferred type becomes more specific.
@fregante your suggestion using tag names rather than the element type will be harder to implement: HTML and SVG elements are in separate maps in the dom lib of typescript. And they even have some common tag names associated to different element types, which would then forbid using one of them. And things would also become harder to support custom elements. Currently, the library supports any kind of |
It can be done, I’ll test this in my other project and I’ll merge it as is for now. Then I’ll think if it’s worth the other part of the change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💙
Now that I think of it, I could use the same package exactly the same way since this module also takes a selector. This way no one probably even needs to specify anything via generics. |
Thank you for this and sorry for the long wait! I'll open a new issue for the other part of the feature |
Instead of using a generic Event type for the argument, the map of event types to the corresponding Event object types is used by the inference.
This is a BC break for projects specifying the generic types explicitly, as it changes the generic signature. For project relying on type inference, the inferred type becomes more specific.