1
1
/**
2
2
* Actions can return an object containing the two properties defined in this interface. Both are optional.
3
3
* - 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.
5
6
* - destroy: Method that is called after the element is unmounted
6
7
*
7
8
* Additionally, you can specify which additional attributes and events the action enables on the applied element.
25
26
*
26
27
* Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action
27
28
*/
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 > > {
29
30
update ?: [ Parameter ] extends [ never ] ? never : ( parameter : Parameter ) => void ;
30
31
destroy ?: ( ) => void ;
31
32
/**
@@ -42,15 +43,21 @@ export interface ActionReturn<Parameter = any, Attributes extends Record<string,
42
43
* The following example defines an action that only works on `<div>` elements
43
44
* and optionally accepts a parameter which it has a default value for:
44
45
* ```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 }) => {
46
47
* // ...
47
48
* }
48
49
* ```
50
+ * `Action<HTMLDivElement>` and `Action<HTMLDiveElement, never>` both signal that the action accepts no parameters.
51
+ *
49
52
* You can return an object with methods `update` and `destroy` from the function and type which additional attributes and events it has.
50
53
* See interface `ActionReturn` for more details.
51
54
*
52
55
* Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action
53
56
*/
54
57
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 > ;
56
59
}
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