-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Make exporting empty functions a way to declare component event dispatchers #5602
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
Comments
this sounds like a big change, i would suggest open a RFC https://github.com/sveltejs/rfcs/ and have a better discussion over there |
I like the idea of dispatching an event without having to deal with it being an actual
|
Hi it might be naive of me thinking this but for me a fix for what @probablykasper said would look like this: <script lang="ts">
export message = createEventDispatcher<string>();//message is of type function ()=> string
message("I just dispatched a message event!");
</script> This would make it possible to create and export empty functions without messing with the type system. I am a little scared that such a feature will not make it into svelte. |
An awesome idea! I too came up with something very similar called Auto-dispatch. But it has added default behaviour which I think is very powerful. Svelte is about being svelte after all and |
Closing as obsolete - createEventDispatcher has been deprecated in Svelte 5, use callback props instead. |
This is a proposal to introduce a more straightforward way to declare component events, namely through the use of empty functions.
Exporting a named function with an empty body would declare an event, and calling that function would dispatch it.
The consumer remains the same
This solution comes as a hybrid between prop functions and
createEventDispatcher
:export function message(){}
export let onMessage = noop
on:
syntax❌wrapped in a CustomEvent
❌must fill type manually
As a side effect this proposal also introduces the ability to rename events when forwarding them:
The implementation is simple, the compiler just has to fill the body of those functions with
createEventDispatcher
's :I cannot think of any realistic scenario where this would be a breaking change.
createEventDispatcher
has been criticized multiple times #2323 #3488 #4584 #5211 #5597. I for one actively avoid using it. The go-to solution appears to be to introduce yet another reserved variable #5598, I see all of those $$variables slowly creeping up on svelte and I'm very much not looking forward to see another.The text was updated successfully, but these errors were encountered: