-
-
Notifications
You must be signed in to change notification settings - Fork 207
(feat) event typings #535
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
(feat) event typings #535
Conversation
WIP - try to find event strings for better autocompletion - support typed createEventDispatcher
… from getting garbage collected
This is an interesting one. The big challenge here of course is that events are essentially global, there is nothing to stop people from dispatching events manually as far as I am aware (via the As a default, I'd err on the side of strictness since most event usage is simple and would be well covered by this feature. Type Errors would generally be warning users of actual problems rather than being a false positive. As for how to configure it, I'm not sure. Is this the kind of thing that should be set globally or on a per file basis? Typescript has set some precedent here in that options are generally project level and opting out of specific behaviour is not really possible (unless you disable TS in its entirety for a line). Should we follow suit and make this a project setting or break with convention and go the eslint way of granular disable rules? |
I also tend to make strict the default. About "how to relax it": I tend towards a per-file-solution like this: <script otherEvents>
// ...
</script> Adding the attribute <script otherEvents="name1,name2">
</script> Which would mean "name1 and name2 are also allowed, but not others". |
I quite like that second option as a middle ground. Do you think |
Good question. |
For reference: halfnelson and Rich are in favor or the "loose" option. |
We will have to fight to the death. There is no other solution. |
Attribute for unknown events seems nice. But I think it would be nice if there's a global or per-project config to disable it, since having to add the attribute every time kind of destroy the purpose of a dispatcher mixin. |
So you also lean towards the "loose" option? What if everything is allowed and only if you add a |
I am ok with strict by default. But for those with a lot of existing dispatch mixin or shared constant of events name, able to turn it off might be easier to gradually migrate to it. |
Mhm maybe loose should be the default then. It feels inconsistent to me that everything's loose, but as soon as you type So we would have something like module.exports = {
sveltePreprocess: ...,
diagnosticOptions: {
strictEvents: true,
} Or on a per-file basis <script strictEvents unknownEvents="additionalEventName1, name2">
//...
</script>
Your thoughts on this? |
I think I'm in the "loose-by-default" camp now. This means the PR can be merged as-is, and we can bikeshed on the options to make it more strict separately. I might even make a RFC with all the type specs that accumulated now to have some official "this is how you do it"-guide for others who might implement IDE support for Svelte. |
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.
Works pretty well. Have a simple suggestion on the string variables.
Also, there are two more situations that you can consider if we should support it.
- import alias for
createEventDispatcher
import { createEventDispatcher as someAlias } from 'svelte'
and then track the alias name like dispatcherName
2. Maybe support something like this
createEventDispatcher()('abc')
The alias is a good catch. The immediately-invoked dispatch is nothing we need to support I think - I tested that in the REPL and no event is fired (I guess it happens too soon), and you must invoke |
#424
To discuss: If the user types
createEventDispatcher
, do we want to be strict and throw type errors when it tries to listen to other events besides those defined in the typings and the bubbled events? Or do we still want to be loose and type all other events asCustomEvent<any>
?So for example if we have
And then use it like
Will this throw a type error the likes of "only click/checked are known" or is it allowed and the type is
CustomEvent<any>
?What do we do if we decide it's an error and it's a false positive? Let's assume the user does
import { clickDispatcher } from ..
which is some kind of events dispatch mixin which makes it possible to listen to another event namedanotherEvent
. How will we make it possible for the user to relax the strictness without having to typeComponentEvents
? Add a<script>
attributehasOtherEvents
which optionally can be given a list of event names?What do we do if we decide it resolves to
CustomEvent<any>
but the user wants strictness? Add a<script>
attributestrictEvents
?@jasonlyu123 @pngwn