-
Notifications
You must be signed in to change notification settings - Fork 40
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
add event bindings to no-incompatible-type-binding rule #101
base: master
Are you sure you want to change the base?
Conversation
185d254
to
b24a0b8
Compare
@runem is this still a thing you haven't done in one of your branches yourself? its been a while since i started work on it so its very possible the functionality just exists now. if not, im happy to finish it off if you can point me in the right direction |
I haven't yet built functionality, so this PR is still relevant :-) Here are my initial thoughts about what needs to be done in order to support this rule: ts-simple-typeThere is still an outstanding issue with how to handle lib-types. Right now, lib-types such as web-component-analyzerA mentioned, we need to extend lit-analyzerWhen type checking events, we could extend the type checking using the lit-analyzer/packages/lit-analyzer/src/analyze/util/type/is-assignable-in-attribute-binding.ts Line 50 in b7fe1b4
Inside the type checking we could have some custom logic that checks for named interfaces like what we returned in |
I just got a simple (and perhaps better) idea we could explore that's even easier to implement. We can implement a function in A nice side-effect of this solution is that all lib-types would all be support in JSDoc at the same time. |
You could probably make use of That does sound like a possible solution, though. Implementing full knowledge of the built-in types would be a huge job so one way or another i think a workaround/shortcut has to exist for now. |
I just published The type of the event is now |
I just merged the |
@43081j Is it okay if I add some commits to this PR? I would love to get this awesome feature into a release :-) |
I'm just about to rebase, but once i do that, go for it |
I rebased. You seem to have it in your head already what we need to do here so feel free to have a go at it. Especially since it crosses over with other changes you've been making recently |
Thanks! I'll take a stab at it now then 👍 |
I've now improved type checking for events (also when we don't know the event type for a given event). In addition, Two considerations I had: 1. If we don't know the exact type of event that a given event handler needs (typeA = ANY), I check for assignability to Examples on invalid error message we get if "strictFunctionTypes" is true and we check against an event with unknown type: 2. I'm in doubt if the |
Here's another point that we need to take into account. When collecting events, I merge events with the same name because events bubble (see picture). Therefore the type of "change" event ends up being a union of all merged events. However, when type checking a parameter with type union, the type checking will fail because parameters are checked contravariant. Here's an example: @customElement("my-element")
export class MyElement extends LitElement {
didUploadFile(file: File) {
this.dispatchEvent(new CustomEvent("change", { detail: file }));
}
render() {
return html`<div @change="${(evt: CustomEvent<File>) => console.log(evt.detail.name)}"></div>`;
}
} The type of the event
To fix this, I introduced a check for "typeA" being equals to UNION. If so, I will check each type in the union against "typeB" resulting in the following type checks: Is One of the types was assignable, so the rule wouldn't report a diagnostic for this type. What do you think about this approach? The nature of event bubbling unfortunately forces the type checking to be a bit more loose, so I don't think that there is a way around this :-) |
awesome ill have a read through the commits. strictFunctionTypesThis stuff is an interesting point. I suppose it is actually right, though... you should strongly type your handlers to the exact subtype of event needed. But i can imagine most people (incl. myself) have plenty of handlers typed that way (just
|
packages/lit-analyzer/src/rules/util/type/is-assignable-in-event-binding.ts
Show resolved
Hide resolved
… 'handleEvent' into account
…types would get incorrectly type checked
…patible-type-binding
@justinfagnani @rictic this feels like something we should still get merged in so i have rebased onto master. but its a mixture of rune's rework and my changes, so i cant say i have a full understanding of whats going on here. from what i remember, we removed the non callable events rule and added support for events to the incompatible types rule (which catches that same thing). but there's also a bunch of stuff rune added for handling unions of events based on bubbling etc. |
here's why i wanted runem/web-component-analyzer#165
the types are always
ANY
and i had to put the jsdoc the wrong way around (compared to anywhere else i saw it in the wild at least).so it doesn't do much at the min but here it is in case WCA starts picking those types up or you have a suggested way i can do it here
i wonder if it should also default to
Event
notany
, but not sure how to do that with simpletype...