@@ -625,7 +625,6 @@ declare module 'svelte/animate' {
625
625
}
626
626
627
627
declare module 'svelte/attachments' {
628
- import type { Action , ActionReturn } from 'svelte/action' ;
629
628
/**
630
629
* An [attachment](https://svelte.dev/docs/svelte/@attach) is a function that runs when an element is mounted
631
630
* to the DOM, and optionally returns a function that is called when the element is later removed.
@@ -691,6 +690,75 @@ declare module 'svelte/attachments' {
691
690
* ```
692
691
* */
693
692
export function fromAction < E extends EventTarget > ( action : Action < E , void > | ( ( element : E ) => void | ActionReturn < void > ) ) : Attachment < E > ;
693
+ /**
694
+ * Actions can return an object containing the two properties defined in this interface. Both are optional.
695
+ * - update: An action can have a parameter. This method will be called whenever that parameter changes,
696
+ * immediately after Svelte has applied updates to the markup. `ActionReturn` and `ActionReturn<undefined>` both
697
+ * mean that the action accepts no parameters.
698
+ * - destroy: Method that is called after the element is unmounted
699
+ *
700
+ * Additionally, you can specify which additional attributes and events the action enables on the applied element.
701
+ * This applies to TypeScript typings only and has no effect at runtime.
702
+ *
703
+ * Example usage:
704
+ * ```ts
705
+ * interface Attributes {
706
+ * newprop?: string;
707
+ * 'on:event': (e: CustomEvent<boolean>) => void;
708
+ * }
709
+ *
710
+ * export function myAction(node: HTMLElement, parameter: Parameter): ActionReturn<Parameter, Attributes> {
711
+ * // ...
712
+ * return {
713
+ * update: (updatedParameter) => {...},
714
+ * destroy: () => {...}
715
+ * };
716
+ * }
717
+ * ```
718
+ */
719
+ interface ActionReturn <
720
+ Parameter = undefined ,
721
+ Attributes extends Record < string , any > = Record < never , any >
722
+ > {
723
+ update ?: ( parameter : Parameter ) => void ;
724
+ destroy ?: ( ) => void ;
725
+ /**
726
+ * ### DO NOT USE THIS
727
+ * This exists solely for type-checking and has no effect at runtime.
728
+ * Set this through the `Attributes` generic instead.
729
+ */
730
+ $$_attributes ?: Attributes ;
731
+ }
732
+
733
+ /**
734
+ * Actions are functions that are called when an element is created.
735
+ * You can use this interface to type such actions.
736
+ * The following example defines an action that only works on `<div>` elements
737
+ * and optionally accepts a parameter which it has a default value for:
738
+ * ```ts
739
+ * export const myAction: Action<HTMLDivElement, { someProperty: boolean } | undefined> = (node, param = { someProperty: true }) => {
740
+ * // ...
741
+ * }
742
+ * ```
743
+ * `Action<HTMLDivElement>` and `Action<HTMLDivElement, undefined>` both signal that the action accepts no parameters.
744
+ *
745
+ * You can return an object with methods `update` and `destroy` from the function and type which additional attributes and events it has.
746
+ * See interface `ActionReturn` for more details.
747
+ */
748
+ interface Action <
749
+ Element = HTMLElement ,
750
+ Parameter = undefined ,
751
+ Attributes extends Record < string , any > = Record < never , any >
752
+ > {
753
+ < Node extends Element > (
754
+ ...args : undefined extends Parameter
755
+ ? [ node : Node , parameter ?: Parameter ]
756
+ : [ node : Node , parameter : Parameter ]
757
+ ) : void | ActionReturn < Parameter , Attributes > ;
758
+ }
759
+
760
+ // Implementation notes:
761
+ // - undefined extends X instead of X extends undefined makes this work better with both strict and nonstrict mode
694
762
695
763
export { } ;
696
764
}
0 commit comments