fix: use typedef instead of exported interface

pull/15933/head
paoloricciuti 4 months ago
parent 35a3b473c7
commit d190509144

@ -1,4 +1,7 @@
/** @import { FromAction } from './public.js' */
/**
* @import { Attachment } from "./public.js";
* @import { ActionReturn } from "svelte/action";
*/
import { noop, render_effect } from 'svelte/internal/client';
import { ATTACHMENT_KEY } from '../constants.js';
import { untrack } from 'svelte';
@ -30,6 +33,22 @@ export function createAttachmentKey() {
return Symbol(ATTACHMENT_KEY);
}
/**
* @template {EventTarget} [Element=HTMLElement]
* @template {*} [Par=unknown]
* @typedef {<Node extends Element, Parameter extends Par>(
* ...args: (undefined extends NoInfer<Parameter>
* ? [
* action: (node: Node, parameter?: never) => void | ActionReturn<Parameter>,
* parameter?: () => NoInfer<Parameter>
* ]
* : [
* action: (node: Node, parameter: Parameter) => void | ActionReturn<Parameter>,
* parameter: () => NoInfer<Parameter>
* ])
* ) => Attachment<Node>} FromAction
*/
/**
* Converts an Action into an Attachment keeping the same behavior. It's useful if you want to start using
* attachments on Components but you have library provided actions.

@ -1,5 +1,3 @@
import type { ActionReturn } from 'svelte/action';
/**
* An [attachment](https://svelte.dev/docs/svelte/@attach) is a function that runs when an element is mounted
* to the DOM, and optionally returns a function that is called when the element is later removed.
@ -11,18 +9,4 @@ export interface Attachment<T extends EventTarget = Element> {
(element: T): void | (() => void);
}
export interface FromAction<Element extends EventTarget = HTMLElement, Par = unknown> {
<Node extends Element, Parameter extends Par>(
...args: undefined extends NoInfer<Parameter>
? [
action: (node: Node, parameter?: never) => void | ActionReturn<Parameter>,
parameter?: () => NoInfer<Parameter>
]
: [
action: (node: Node, parameter: Parameter) => void | ActionReturn<Parameter>,
parameter: () => NoInfer<Parameter>
]
): Attachment<Node>;
}
export * from './index.js';

@ -636,20 +636,6 @@ declare module 'svelte/attachments' {
export interface Attachment<T extends EventTarget = Element> {
(element: T): void | (() => void);
}
export interface FromAction<Element extends EventTarget = HTMLElement, Par = unknown> {
<Node extends Element, Parameter extends Par>(
...args: undefined extends NoInfer<Parameter>
? [
action: (node: Node, parameter?: never) => void | ActionReturn<Parameter>,
parameter?: () => NoInfer<Parameter>
]
: [
action: (node: Node, parameter: Parameter) => void | ActionReturn<Parameter>,
parameter: () => NoInfer<Parameter>
]
): Attachment<Node>;
}
/**
* Creates an object key that will be recognised as an attachment when the object is spread onto an element,
* as a programmatic alternative to using `{@attach ...}`. This can be useful for library authors, though
@ -673,49 +659,8 @@ declare module 'svelte/attachments' {
* @since 5.29
*/
export function createAttachmentKey(): symbol;
export function fromAction<Node extends HTMLElement, Parameter extends any>(...args: undefined extends NoInfer<Parameter> ? [action: (node: Node, parameter?: never) => void | ActionReturn_1<Parameter, Record<never, any>>, parameter?: (() => NoInfer<Parameter>) | undefined] : [action: (node: Node, parameter: Parameter) => void | ActionReturn_1<Parameter, Record<never, any>>, parameter: () => NoInfer<Parameter>]): Attachment<Node>;
/**
* Actions can return an object containing the two properties defined in this interface. Both are optional.
* - update: An action can have a parameter. This method will be called whenever that parameter changes,
* immediately after Svelte has applied updates to the markup. `ActionReturn` and `ActionReturn<undefined>` both
* mean that the action accepts no parameters.
* - destroy: Method that is called after the element is unmounted
*
* Additionally, you can specify which additional attributes and events the action enables on the applied element.
* This applies to TypeScript typings only and has no effect at runtime.
*
* Example usage:
* ```ts
* interface Attributes {
* newprop?: string;
* 'on:event': (e: CustomEvent<boolean>) => void;
* }
*
* export function myAction(node: HTMLElement, parameter: Parameter): ActionReturn<Parameter, Attributes> {
* // ...
* return {
* update: (updatedParameter) => {...},
* destroy: () => {...}
* };
* }
* ```
*/
interface ActionReturn_1<
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;
}
// Implementation notes:
// - undefined extends X instead of X extends undefined makes this work better with both strict and nonstrict mode
export function fromAction<Node extends HTMLElement, Parameter extends unknown>(...args: undefined extends NoInfer<Parameter> ? [action: (node: Node, parameter?: never) => void | ActionReturn<Parameter, any>, parameter?: (() => NoInfer<Parameter>) | undefined] : [action: (node: Node, parameter: Parameter) => void | ActionReturn<Parameter, any>, parameter: () => NoInfer<Parameter>]): Attachment<Node>;
export type FromAction<Element extends EventTarget = HTMLElement, Par extends unknown = unknown> = <Node extends Element, Parameter extends Par>(...args: (undefined extends NoInfer<Parameter> ? [action: (node: Node, parameter?: never) => void | ActionReturn<Parameter>, parameter?: () => NoInfer<Parameter>] : [action: (node: Node, parameter: Parameter) => void | ActionReturn<Parameter>, parameter: () => NoInfer<Parameter>])) => Attachment<Node>;
export {};
}

Loading…
Cancel
Save