Skip to content

Commit 5320874

Browse files
authored
docs
1 parent 5ee6087 commit 5320874

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/runtime/action/index.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* Actions can return an object containing the two properties defined in this interface. Both are optional.
33
* - update: An action can have a parameter. This method will be called whenever that parameter changes,
4-
* immediately after Svelte has applied updates to the markup.
4+
* immediately after Svelte has applied updates to the markup. `ActionReturn` and `ActionReturn<never>` both
5+
* mean that the action accepts no parameters, which makes it illegal to set the `update` method.
56
* - destroy: Method that is called after the element is unmounted
67
*
78
* Additionally, you can specify which additional attributes and events the action enables on the applied element.
@@ -25,7 +26,7 @@
2526
*
2627
* Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action
2728
*/
28-
export interface ActionReturn<Parameter = any, Attributes extends Record<string, any> = Record<never, any>> {
29+
export interface ActionReturn<Parameter = never, Attributes extends Record<string, any> = Record<never, any>> {
2930
update?: [Parameter] extends [never] ? never : (parameter: Parameter) => void;
3031
destroy?: () => void;
3132
/**
@@ -42,15 +43,21 @@ export interface ActionReturn<Parameter = any, Attributes extends Record<string,
4243
* The following example defines an action that only works on `<div>` elements
4344
* and optionally accepts a parameter which it has a default value for:
4445
* ```ts
45-
* export const myAction: Action<HTMLDivElement, { someProperty: boolean }> = (node, param = { someProperty: true }) => {
46+
* export const myAction: Action<HTMLDivElement, { someProperty: boolean } | undefined> = (node, param = { someProperty: true }) => {
4647
* // ...
4748
* }
4849
* ```
50+
* `Action<HTMLDivElement>` and `Action<HTMLDiveElement, never>` both signal that the action accepts no parameters.
51+
*
4952
* You can return an object with methods `update` and `destroy` from the function and type which additional attributes and events it has.
5053
* See interface `ActionReturn` for more details.
5154
*
5255
* Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action
5356
*/
5457
export interface Action<Element = HTMLElement, Parameter = never, Attributes extends Record<string, any> = Record<never, any>> {
55-
<Node extends Element>(...args: [Parameter] extends [never ]? [node: Node] : undefined extends Parameter? [node: Node, parameter?: Parameter] : [node: Node, parameter: Parameter]): void | ActionReturn<Parameter, Attributes>;
58+
<Node extends Element>(...args: [Parameter] extends [never] ? [node: Node] : undefined extends Parameter? [node: Node, parameter?: Parameter] : [node: Node, parameter: Parameter]): void | ActionReturn<Parameter, Attributes>;
5659
}
60+
61+
// Implementation notes:
62+
// - undefined extends X instead of X extends undefined makes this work better with both strict and nonstrict mode
63+
// - [X] extends [never] is needed, X extends never would reduce the whole resulting type to never and not to one of the condition outcomes

0 commit comments

Comments
 (0)