{ globalattribute?: string; } // add a new attribute for button elements export interface HTMLButtonAttributes { veryexperimentalattribute?: string; } } export {}; // ensure this is not an ambient module, else types will be overridden instead of augmented ``` Then make sure that the `d.ts` file is referenced in your `tsconfig.json`. If it reads something like `"include": ["src/**/*"]` and your `d.ts` file is inside `src`, it should work. You may need to reload for the changes to take effect.
## docs/svelte/98-reference/index.md
--- title: Reference ---
## docs/svelte/98-reference/20-svelte.md
--- title: svelte --- ```js // @noErrors import { SvelteComponent, SvelteComponentTyped, afterUpdate, beforeUpdate, createContext, createEventDispatcher, createRawSnippet, flushSync, fork, getAbortSignal, getAllContexts, getContext, hasContext, hydratable, hydrate, mount, onDestroy, onMount, setContext, settled, tick, unmount, untrack } from 'svelte'; ``` ## SvelteComponent This was the base class for Svelte components in Svelte 4. Svelte 5+ components are completely different under the hood. For typing, use `Component` instead. To instantiate components, use `mount` instead. See [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more info. ```dts class SvelteComponent< Props extends Record
= Record, Events extends Record = any, Slots extends Record = any > {/*…*/} ``` ```dts static element?: typeof HTMLElement; ```
The custom element version of the component. Only present if compiled with the `customElement` compiler option
```dts [prop: string]: any; ```
```dts constructor(options: ComponentConstructorOptions
>); ``` - deprecated This constructor only exists when using the `asClassComponent` compatibility helper, which is a stop-gap solution. Migrate towards using `mount` instead. See [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more info.
```dts $destroy(): void; ```
- deprecated This method only exists when using one of the legacy compatibility helpers, which is a stop-gap solution. See [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more info.
```dts $on
>( type: K, callback: (e: Events[K]) => void ): () => void; ``` - deprecated This method only exists when using one of the legacy compatibility helpers, which is a stop-gap solution. See [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more info.
```dts $set(props: Partial
): void; ``` - deprecated This method only exists when using one of the legacy compatibility helpers, which is a stop-gap solution. See [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more info.
## SvelteComponentTyped Use `Component` instead. See [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more information. ```dts class SvelteComponentTyped< Props extends Record
= Record, Events extends Record = any, Slots extends Record = any > extends SvelteComponent {} ``` ## afterUpdate Use [`$effect`](/docs/svelte/$effect) instead Schedules a callback to run immediately after the component has been updated. The first time the callback runs will be after the initial `onMount`. In runes mode use `$effect` instead. ```dts function afterUpdate(fn: () => void): void; ```
## beforeUpdate Use [`$effect.pre`](/docs/svelte/$effect#$effect.pre) instead Schedules a callback to run immediately before the component is updated after any state change. The first time the callback runs will be before the initial `onMount`. In runes mode use `$effect.pre` instead. ```dts function beforeUpdate(fn: () => void): void; ```
## createContext Available since 5.40.0 Returns a `[get, set]` pair of functions for working with context in a type-safe way. `get` will throw an error if no parent component called `set`. ```dts function createContext(): [() => T, (context: T) => T]; ```
## createEventDispatcher Use callback props and/or the `$host()` rune instead — see [migration guide](/docs/svelte/v5-migration-guide#Event-changes-Component-events) Creates an event dispatcher that can be used to dispatch [component events](/docs/svelte/legacy-on#Component-events). Event dispatchers are functions that can take two arguments: `name` and `detail`. Component events created with `createEventDispatcher` create a [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent). These events do not [bubble](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture). The `detail` argument corresponds to the [CustomEvent.detail](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/detail) property and can contain any type of data. The event dispatcher can be typed to narrow the allowed event names and the type of the `detail` argument: ```ts const dispatch = createEventDispatcher<{ loaded: null; // does not take a detail argument change: string; // takes a detail argument of type string, which is required optional: number | null; // takes an optional detail argument of type number }>(); ``` ```dts function createEventDispatcher< EventMap extends Record = any >(): EventDispatcher; ```
## createRawSnippet Create a snippet programmatically ```dts function createRawSnippet
( fn: (...params: Getters) => { render: () => string; setup?: (element: Element) => void | (() => void); } ): Snippet; ``` ## flushSync Synchronously flush any pending updates. Returns void if no callback is provided, otherwise returns the result of calling the callback. ```dts function flushSync(fn?: (() => T) | undefined): T; ```
## fork Available since 5.42 Creates a 'fork', in which state changes are evaluated but not applied to the DOM. This is useful for speculatively loading data (for example) when you suspect that the user is about to take some action. Frameworks like SvelteKit can use this to preload data when the user touches or hovers over a link, making any subsequent navigation feel instantaneous. The `fn` parameter is a synchronous function that modifies some state. The state changes will be reverted after the fork is initialised, then reapplied if and when the fork is eventually committed. When it becomes clear that a fork will _not_ be committed (e.g. because the user navigated elsewhere), it must be discarded to avoid leaking memory. ```dts function fork(fn: () => void): Fork; ```
## getAbortSignal Returns an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) that aborts when the current [derived](/docs/svelte/$derived) or [effect](/docs/svelte/$effect) re-runs or is destroyed. Must be called while a derived or effect is running. ```svelte ``` ```dts function getAbortSignal(): AbortSignal; ```
## getAllContexts Retrieves the whole context map that belongs to the closest parent component. Must be called during component initialisation. Useful, for example, if you programmatically create a component and want to pass the existing context to it. ```dts function getAllContexts< T extends Map
= Map >(): T; ``` ## getContext Retrieves the context that belongs to the closest parent component with the specified `key`. Must be called during component initialisation. [`createContext`](/docs/svelte/svelte#createContext) is a type-safe alternative. ```dts function getContext(key: any): T; ```
## hasContext Checks whether a given `key` has been set in the context of a parent component. Must be called during component initialisation. ```dts function hasContext(key: any): boolean; ```
## hydratable ```dts function hydratable(key: string, fn: () => T): T; ```
## hydrate Hydrates a component on the given target and returns the exports and potentially the props (if compiled with `accessors: true`) of the component ```dts function hydrate< Props extends Record
, Exports extends Record >( component: | ComponentType> | Component, options: {} extends Props ? { target: Document | Element | ShadowRoot; props?: Props; events?: Record any>; context?: Map; intro?: boolean; recover?: boolean; transformError?: (error: unknown) => unknown; } : { target: Document | Element | ShadowRoot; props: Props; events?: Record any>; context?: Map; intro?: boolean; recover?: boolean; transformError?: (error: unknown) => unknown; } ): Exports; ``` ## mount Mounts a component to the given target and returns the exports and potentially the props (if compiled with `accessors: true`) of the component. Transitions will play during the initial render unless the `intro` option is set to `false`. ```dts function mount< Props extends Record
, Exports extends Record >( component: | ComponentType> | Component, options: MountOptions ): Exports; ``` ## onDestroy Schedules a callback to run immediately before the component is unmounted. Out of `onMount`, `beforeUpdate`, `afterUpdate` and `onDestroy`, this is the only one that runs inside a server-side component. ```dts function onDestroy(fn: () => any): void; ```
## onMount `onMount`, like [`$effect`](/docs/svelte/$effect), schedules a function to run as soon as the component has been mounted to the DOM. Unlike `$effect`, the provided function only runs once. It must be called during the component's initialisation (but doesn't need to live _inside_ the component; it can be called from an external module). If a function is returned _synchronously_ from `onMount`, it will be called when the component is unmounted. `onMount` functions do not run during [server-side rendering](/docs/svelte/svelte-server#render). ```dts function onMount( fn: () => | NotFunction | Promise> | (() => any) ): void; ```
## setContext Associates an arbitrary `context` object with the current component and the specified `key` and returns that object. The context is then available to children of the component (including slotted content) with `getContext`. Like lifecycle functions, this must be called during component initialisation. [`createContext`](/docs/svelte/svelte#createContext) is a type-safe alternative. ```dts function setContext(key: any, context: T): T; ```
## settled Available since 5.36 Returns a promise that resolves once any state changes, and asynchronous work resulting from them, have resolved and the DOM has been updated ```dts function settled(): Promise; ```
## tick Returns a promise that resolves once any pending state changes have been applied. ```dts function tick(): Promise; ```
## unmount Unmounts a component that was previously mounted using `mount` or `hydrate`. Since 5.13.0, if `options.outro` is `true`, [transitions](/docs/svelte/transition) will play before the component is removed from the DOM. Returns a `Promise` that resolves after transitions have completed if `options.outro` is true, or immediately otherwise (prior to 5.13.0, returns `void`). ```js // @errors: 7031 import { mount, unmount } from 'svelte'; import App from './App.svelte'; const app = mount(App, { target: document.body }); // later... unmount(app, { outro: true }); ``` ```dts function unmount( component: Record, options?: | { outro?: boolean; } | undefined ): Promise; ```
## untrack When used inside a [`$derived`](/docs/svelte/$derived) or [`$effect`](/docs/svelte/$effect), any state read inside `fn` will not be treated as a dependency. ```ts $effect(() => { // this will run when `data` changes, but not when `time` changes save(data, { timestamp: untrack(() => time) }); }); ``` ```dts function untrack(fn: () => T): T; ```
## Component Can be used to create strongly typed Svelte components. #### Example: You have component library on npm called `component-library`, from which you export a component called `MyComponent`. For Svelte+TypeScript users, you want to provide typings. Therefore you create a `index.d.ts`: ```ts import type { Component } from 'svelte'; export declare const MyComponent: Component<{ foo: string }> {} ``` Typing this makes it possible for IDEs like VS Code with the Svelte extension to provide intellisense and to use the component like this in a Svelte file with TypeScript: ```svelte ``` ```dts interface Component< Props extends Record
= {}, Exports extends Record = {}, Bindings extends keyof Props | '' = string > {/*…*/} ``` ```dts ( this: void, internals: ComponentInternals, props: Props ): { /** * @deprecated This method only exists when using one of the legacy compatibility helpers, which * is a stop-gap solution. See [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) * for more info. */ $on?(type: string, callback: (e: any) => void): () => void; /** * @deprecated This method only exists when using one of the legacy compatibility helpers, which * is a stop-gap solution. See [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) * for more info. */ $set?(props: Partial
): void; } & Exports; ``` - `internal` An internal object used by Svelte. Do not use or modify. - `props` The props passed to the component.
```dts element?: typeof HTMLElement; ```
The custom element version of the component. Only present if compiled with the `customElement` compiler option
## ComponentConstructorOptions In Svelte 4, components are classes. In Svelte 5, they are functions. Use `mount` instead to instantiate components. See [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more info. ```dts interface ComponentConstructorOptions< Props extends Record
= Record > {/*…*/} ``` ```dts target: Element | Document | ShadowRoot; ```
```dts anchor?: Element; ```
```dts props?: Props; ```
```dts context?: Map
; ```
```dts hydrate?: boolean; ```
```dts intro?: boolean; ```
```dts recover?: boolean; ```
```dts sync?: boolean; ```
```dts idPrefix?: string; ```
```dts $$inline?: boolean; ```
```dts transformError?: (error: unknown) => unknown; ```
## ComponentEvents The new `Component` type does not have a dedicated Events type. Use `ComponentProps` instead. ```dts type ComponentEvents
= Comp extends SvelteComponent ? Events : never; ``` ## ComponentInternals Internal implementation details that vary between environments ```dts type ComponentInternals = Branded<{}, 'ComponentInternals'>; ```
## ComponentProps Convenience type to get the props the given component expects. Example: Ensure a variable contains the props expected by `MyComponent`: ```ts import type { ComponentProps } from 'svelte'; import MyComponent from './MyComponent.svelte'; // Errors if these aren't the correct props expected by MyComponent. const props: ComponentProps = { foo: 'bar' }; ``` Example: A generic function that accepts some component and infers the type of its props: ```ts import type { Component, ComponentProps } from 'svelte'; import MyComponent from './MyComponent.svelte'; function withProps>( component: TComponent, props: ComponentProps ) {}; // Errors if the second argument is not the correct props expected by the component in the first argument. withProps(MyComponent, { foo: 'bar' }); ``` ```dts type ComponentProps< Comp extends SvelteComponent | Component
> = Comp extends SvelteComponent ? Props : Comp extends Component ? Props : never; ``` ## ComponentType This type is obsolete when working with the new `Component` type. ```dts type ComponentType< Comp extends SvelteComponent = SvelteComponent > = (new ( options: ComponentConstructorOptions< Comp extends SvelteComponent ? Props : Record > ) => Comp) & { /** The custom element version of the component. Only present if compiled with the `customElement` compiler option */ element?: typeof HTMLElement; }; ```
## EventDispatcher ```dts interface EventDispatcher< EventMap extends Record
> {/*…*/} ``` ```dts
( ...args: null extends EventMap[Type] ? [type: Type, parameter?: EventMap[Type] | null | undefined, options?: DispatchOptions] : undefined extends EventMap[Type] ? [type: Type, parameter?: EventMap[Type] | null | undefined, options?: DispatchOptions] : [type: Type, parameter: EventMap[Type], options?: DispatchOptions] ): boolean; ```
## Fork Available since 5.42 Represents work that is happening off-screen, such as data being preloaded in anticipation of the user navigating ```dts interface Fork {/*…*/} ```
```dts commit(): Promise
; ``` Commit the fork. The promise will resolve once the state change has been applied
```dts discard(): void; ```
Discard the fork
## MountOptions Defines the options accepted by the `mount()` function. ```dts type MountOptions< Props extends Record
= Record > = { /** * Target element where the component will be mounted. */ target: Document | Element | ShadowRoot; /** * Optional node inside `target`. When specified, it is used to render the component immediately before it. */ anchor?: Node; /** * Allows the specification of events. * @deprecated Use callback props instead. */ events?: Record any>; /** * Can be accessed via `getContext()` at the component level. */ context?: Map; /** * Whether or not to play transitions on initial render. * @default true */ intro?: boolean; /** * A function that transforms errors caught by error boundaries before they are passed to the `failed` snippet. * Defaults to the identity function. */ transformError?: ( error: unknown ) => unknown | Promise; } & ({} extends Props ? { /** * Component properties. */ props?: Props; } : { /** * Component properties. */ props: Props; }); ``` ## Snippet The type of a `#snippet` block. You can use it to (for example) express that your component expects a snippet of a certain type: ```ts let { banner }: { banner: Snippet<[{ text: string }]> } = $props(); ``` You can only call a snippet through the `{@render ...}` tag. See the [snippet documentation](/docs/svelte/snippet) for more info. ```dts interface Snippet
{/*…*/} ``` ```dts ( this: void, // this conditional allows tuples but not arrays. Arrays would indicate a // rest parameter type, which is not supported. If rest parameters are added // in the future, the condition can be removed. ...args: number extends Parameters['length'] ? never : Parameters ): { '{@render ...} must be called with a Snippet': "import type { Snippet } from 'svelte'"; } & typeof SnippetReturn; ```
## docs/svelte/98-reference/21-svelte-action.md
--- title: svelte/action --- This module provides types for [actions](use), which have been superseded by [attachments](@attach). ## Action Actions are functions that are called when an element is created. You can use this interface to type such actions. The following example defines an action that only works on `` elements and optionally accepts a parameter which it has a default value for: ```ts export const myAction: Action
= (node, param = { someProperty: true }) => { // ... } ``` `Action` and `Action` both signal that the action accepts no parameters. You can return an object with methods `update` and `destroy` from the function and type which additional attributes and events it has. See interface `ActionReturn` for more details. ```dts interface Action< Element = HTMLElement, Parameter = undefined, Attributes extends Record
= Record< never, any > > {/*…*/} ``` ```dts
( ...args: undefined extends Parameter ? [node: Node, parameter?: Parameter] : [node: Node, parameter: Parameter] ): void | ActionReturn; ```
## ActionReturn 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` 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) => void; } export function myAction(node: HTMLElement, parameter: Parameter): ActionReturn { // ... return { update: (updatedParameter) => {...}, destroy: () => {...} }; } ``` ```dts interface ActionReturn< Parameter = undefined, Attributes extends Record
= Record< never, any > > {/*…*/} ``` ```dts update?: (parameter: Parameter) => void; ```
```dts destroy?: () => void; ```
## docs/svelte/98-reference/21-svelte-animate.md
--- title: svelte/animate --- ```js // @noErrors import { flip } from 'svelte/animate'; ``` ## flip The flip function calculates the start and end position of an element and animates between them, translating the x and y values. `flip` stands for [First, Last, Invert, Play](https://aerotwist.com/blog/flip-your-animations/). ```dts function flip( node: Element, { from, to }: { from: DOMRect; to: DOMRect; }, params?: FlipParams ): AnimationConfig; ```
## AnimationConfig ```dts interface AnimationConfig {/*…*/} ```
```dts delay?: number; ```
```dts duration?: number; ```
```dts easing?: (t: number) => number; ```
```dts css?: (t: number, u: number) => string; ```
```dts tick?: (t: number, u: number) => void; ```
## FlipParams ```dts interface FlipParams {/*…*/} ```
```dts delay?: number; ```
```dts duration?: number | ((len: number) => number); ```
```dts easing?: (t: number) => number; ```
## docs/svelte/98-reference/21-svelte-attachments.md
--- title: svelte/attachments tags: attachments --- ```js // @noErrors import { createAttachmentKey, fromAction } from 'svelte/attachments'; ``` ## createAttachmentKey Available since 5.29 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 is generally not needed when building an app. ```svelte click me ``` ```dts function createAttachmentKey(): symbol; ```
## fromAction Converts an [action](/docs/svelte/use) into an [attachment](/docs/svelte/@attach) keeping the same behavior. It's useful if you want to start using attachments on components but you have actions provided by a library. Note that the second argument, if provided, must be a function that _returns_ the argument to the action function, not the argument itself. ```svelte ...
bar)}>...
``` ```dts function fromAction< E extends EventTarget, T extends unknown >( action: | Action | ((element: E, arg: T) => void | ActionReturn), fn: () => T ): Attachment; ```
```dts function fromAction( action: | Action | ((element: E) => void | ActionReturn) ): Attachment; ```
## Attachment An [attachment](/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. It can be attached to an element with an `{@attach ...}` tag, or by spreading an object containing a property created with [`createAttachmentKey`](/docs/svelte/svelte-attachments#createAttachmentKey). ```dts interface Attachment
{/*…*/} ``` ```dts (element: T): void | (() => void); ```
## docs/svelte/98-reference/21-svelte-compiler.md
--- title: svelte/compiler --- ```js // @noErrors import { VERSION, compile, compileModule, migrate, parse, parseCss, preprocess, print, walk } from 'svelte/compiler'; ``` ## VERSION The current version, as set in package.json. ```dts const VERSION: string; ```
## compile `compile` converts your `.svelte` source code into a JavaScript module that exports a component ```dts function compile( source: string, options: CompileOptions ): CompileResult; ```
## compileModule `compileModule` takes your JavaScript source code containing runes, and turns it into a JavaScript module. ```dts function compileModule( source: string, options: ModuleCompileOptions ): CompileResult; ```
## migrate Does a best-effort migration of Svelte code towards using runes, event attributes and render tags. May throw an error if the code is too complex to migrate automatically. ```dts function migrate( source: string, { filename, use_ts }?: | { filename?: string; use_ts?: boolean; } | undefined ): { code: string; }; ```
## parse The parse function parses a component, returning only its abstract syntax tree. The `modern` option (`false` by default in Svelte 5) makes the parser return a modern AST instead of the legacy AST. `modern` will become `true` by default in Svelte 6, and the option will be removed in Svelte 7. ```dts function parse( source: string, options: { filename?: string; modern: true; loose?: boolean; } ): AST.Root; ```
```dts function parse( source: string, options?: | { filename?: string; modern?: false; loose?: boolean; } | undefined ): Record; ```
## parseCss The parseCss function parses a CSS stylesheet, returning its abstract syntax tree. ```dts function parseCss(source: string): AST.CSS.StyleSheetFile; ```
## preprocess The preprocess function provides convenient hooks for arbitrarily transforming component source code. For example, it can be used to convert a `