-
-
Couldn't load subscription status.
- Fork 4.7k
Description
Describe the problem
Before Svelte attachment, Svelte action allows specifying additional attributes (including custom event listeners) on the element it is placed on:
svelte/packages/svelte/src/action/public.d.ts
Lines 27 to 38 in 5912754
| export interface ActionReturn< | |
| Parameter = undefined, | |
| Attributes extends Record<string, any> = Record<never, any> | |
| > { | |
| update?: (parameter: Parameter) => void; | |
| destroy?: () => void; | |
| /** | |
| * ### DO NOT USE THIS | |
| * This exists solely for type-checking and has no effect at runtime. | |
| * Set this through the `Attributes` generic instead. | |
| */ | |
| $$_attributes?: Attributes; |
For example:
<script lang="ts">
import { type ActionReturn } from 'svelte/action';
interface ClickOutsideAttributes {
musthave: string;
onclickoutside?: (event: CustomEvent) => void;
}
function clickoutside(node: Element): ActionReturn<void, ClickOutsideAttributes> {
/* ... */
return {};
}
</script>
<dialog use:clickoutside musthave="something">
<!-- ... -->
</dialog>As one would expect, the editor tooling will pick up onclickoutside as optional, and musthave as required - a nice ergonomics for element enhancement. I am missing this handy feature when using attachment, especially as a lib author.
Having this level of feature parity is also nice to encourage attachment over action, which I think is the right direction going forward. The action docs seems to suggest so too:
This module provides types for actions, which have been superseded by attachments.
Describe the proposed solution
The current Attachment interface...
svelte/packages/svelte/src/attachments/public.d.ts
Lines 8 to 10 in bde51ed
| export interface Attachment<T extends EventTarget = Element> { | |
| (element: T): void | (() => void); | |
| } |
...can perhaps become:
export interface Attachment<
Element extends EventTarget = Element,
Attributes extends Record<string, any> = Record<never, any>,
> {
(element: Element): void | AttachmentReturn<Attributes>
}
export interface AttachmentReturn<
Attributes extends Record<string, any> = Record<never, any>
> {
(): void;
/**
* ### DO NOT USE THIS
* This exists solely for type-checking and has no effect at runtime.
* Set this through the `Attributes` generic instead.
*/
$$_attributes?: Attributes;
}fromAction can infer accordingly.
I suspect, however, much of the work required for this is on the language-tools side. Svelte attachment can also live within an object being spread onto the element, and I am not sure if that indicates any additional complexity.
Related artifacts:
- (feat) enable actions to enhance typings on applied element language-tools#1553
- feat: attachments #15000
- [TypeScript] Action Type #6538
Originally discussed in #16676
Importance
nice to have