-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Svelte 5: export generic EventHandler
type
#13062
Comments
While there isn't much reason against exporting it, the type isn't that important either. It's more of a utility type for a more concise type definition. It's not required for typing props. You can just type it as |
Didn't know about the I am typing props for my library, and I thought E2 was better for DX, but it was a bit too long. // NOTE Svelte uses `any` for the return type
type E1 = (e: Event) => void;
type E2 = (e: Event & { currentTarget: EventTarget & HTMLDialogElement }) => void; |
Are you forwarding everything to the dialog element? In that case, extending from |
I am forwarding
import type { HTMLDialogAttributes } from 'svelte/elements';
type Handlers = Pick<HTMLDialogAttributes, 'onclose' | 'oncancel'>; |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Hello, You can use let {
showModal = $bindable<boolean>(),
closeWithBackdrop = false,
preventCancel = false,
showTransition = true,
oncancel,
onclose,
children
}: { showModal: boolean }
+ & Pick<HTMLDialogAttributes, 'oncancel' | 'onclose'>
& Partial<{
closeWithBackdrop: boolean;
preventCancel: boolean;
showTransition: boolean;
- oncancel: EventHandler<Event, HTMLDialogElement>;
- onclose: EventHandler<Event, HTMLDialogElement>;
children: Snippet;
}> = $props(); |
This comment was marked as resolved.
This comment was marked as resolved.
This is definitely a documentation issue, the new Svelte 5 docs should have a section dedicated to typing props with omit, pick |
Describe the problem
Since DOM event forwarding is removed, event handlers must be typed in props:
Svelte exports multiple event handlers, but the generic
EventHandler
is not one of them.However, it is required to define these event handlers:
Describe the proposed solution
EventHandler
as isGenericEventHandler
asEventHandler<Event, T>
Importance
nice to have
The text was updated successfully, but these errors were encountered: