メインコンテンツにスキップ

svelte

import {
	class SvelteComponent<Props extends Record<string, any> = Record<string, any>, Events extends Record<string, any> = any, Slots extends Record<string, any> = any>

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 for more info.

SvelteComponent
,
class SvelteComponentTyped<Props extends Record<string, any> = Record<string, any>, Events extends Record<string, any> = any, Slots extends Record<string, any> = any>
@deprecatedUse Component instead. See migration guide for more information.
SvelteComponentTyped
,
function afterUpdate(fn: () => void): void

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.

@deprecatedUse $effect instead — see https://svelte.dokyumento.jp/docs/svelte/$effect
afterUpdate
,
function beforeUpdate(fn: () => void): void

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.

@deprecatedUse $effect.pre instead — see https://svelte.dokyumento.jp/docs/svelte/$effect#$effect.pre
beforeUpdate
,
function createEventDispatcher<EventMap extends Record<string, any> = any>(): EventDispatcher<EventMap>

Creates an event dispatcher that can be used to dispatch component events. Event dispatchers are functions that can take two arguments: name and detail.

Component events created with createEventDispatcher create a CustomEvent. These events do not bubble. The detail argument corresponds to the 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:

const dispatch = createEventDispatcher&#x3C;{
 loaded: never; // 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
}>();
@deprecatedUse callback props and/or the $host() rune instead — see https://svelte.dokyumento.jp/docs/svelte/v5-migration-guide#Event-changes-Component-events
createEventDispatcher
,
function createRawSnippet<Params extends unknown[]>(fn: (...params: Getters<Params>) => {
    render: () => string;
    setup?: (element: Element) => void | (() => void);
}): Snippet<Params>

Create a snippet programmatically

createRawSnippet
,
function flushSync(fn?: (() => void) | undefined): void

Synchronously flushes any pending state changes and those that result from it.

flushSync
,
function getAllContexts<T extends Map<any, any> = Map<any, any>>(): T

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.

getAllContexts
,
function getContext<T>(key: any): T

Retrieves the context that belongs to the closest parent component with the specified key. Must be called during component initialisation.

getContext
,
function hasContext(key: any): boolean

Checks whether a given key has been set in the context of a parent component. Must be called during component initialisation.

hasContext
,
function hydrate<Props extends Record<string, any>, Exports extends Record<string, any>>(component: ComponentType<SvelteComponent<Props>> | Component<Props, Exports, any>, options: {} extends Props ? {
    target: Document | Element | ShadowRoot;
    props?: Props;
    events?: Record<string, (e: any) => any>;
    context?: Map<any, any>;
    intro?: boolean;
    recover?: boolean;
} : {
    target: Document | Element | ShadowRoot;
    props: Props;
    events?: Record<string, (e: any) => any>;
    context?: Map<any, any>;
    intro?: boolean;
    recover?: boolean;
}): Exports

Hydrates a component on the given target and returns the exports and potentially the props (if compiled with accessors: true) of the component

hydrate
,
function mount<Props extends Record<string, any>, Exports extends Record<string, any>>(component: ComponentType<SvelteComponent<Props>> | Component<Props, Exports, any>, options: MountOptions<Props>): Exports

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.

mount
,
function onDestroy(fn: () => any): void

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.

onDestroy
,
function onMount<T>(fn: () => NotFunction<T> | Promise<NotFunction<T>> | (() => any)): void

The onMount function schedules a callback to run as soon as the component has been mounted to the DOM. 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 does not run inside server-side components.

onMount
,
function setContext<T>(key: any, context: T): T

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.

setContext
,
function tick(): Promise<void>

Returns a promise that resolves once any pending state changes have been applied.

tick
,
function unmount(component: Record<string, any>): void

Unmounts a component that was previously mounted using mount or hydrate.

unmount
,
function untrack<T>(fn: () => T): T

When used inside a $derived or $effect, any state read inside fn will not be treated as a dependency.

$effect(() => {
  // this will run when `data` changes, but not when `time` changes
  save(data, {
	timestamp: untrack(() => time)
  });
});
untrack
} from 'svelte';

SvelteComponent

これは Svelte 4 における Svelte コンポーネントの基底クラスでした。Svelte 5 以降のコンポーネントは内部的に完全に異なります。型付けには、代わりに Component を使用してください。コンポーネントをインスタンス化するには、代わりに mount を使用してください。詳細については、移行ガイドを参照してください。

class SvelteComponent<
	Props extends Record<string, any> = Record<string, any>,
	Events extends Record<string, any> = any,
	Slots extends Record<string, any> = any
> {}
static element?: typeof HTMLElement;

コンポーネントのカスタム要素バージョン。customElement コンパイラオプションでコンパイルされた場合にのみ存在します

[prop: string]: any;
constructor(options: ComponentConstructorOptions<Properties<Props, Slots>>);
$destroy(): void;
$on<K extends Extract<keyof Events, string>>(
	type: K,
	callback: (e: Events[K]) => void
): () => void;
$set(props: Partial<Props>): void;

SvelteComponentTyped

代わりに Component を使用してください。詳細については、移行ガイドを参照してください。

class SvelteComponentTyped<
	Props extends Record<string, any> = Record<string, any>,
	Events extends Record<string, any> = any,
	Slots extends Record<string, any> = any
> extends SvelteComponent<Props, Events, Slots> {}

afterUpdate

代わりに $effect を使用してください。— https://svelte.dokyumento.jp/docs/svelte/$effect を参照してください

コンポーネントが更新された直後に実行されるコールバックをスケジュールします。

コールバックが最初に実行されるのは、最初の onMount の後になります。

ルーンモードでは、代わりに $effect を使用してください。

function afterUpdate(fn: () => void): void;

beforeUpdate

代わりに $effect.pre を使用してください。— https://svelte.dokyumento.jp/docs/svelte/$effect#$effect.pre を参照してください

状態が変更された後、コンポーネントが更新される直前に実行されるコールバックをスケジュールします。

コールバックが最初に実行されるのは、最初の onMount の前になります。

ルーンモードでは、代わりに $effect.pre を使用してください。

function beforeUpdate(fn: () => void): void;

createEventDispatcher

代わりにコールバックプロパティおよび/または $host() ルーンを使用してください。— https://svelte.dokyumento.jp/docs/svelte/v5-migration-guide#Event-changes-Component-events を参照してください

コンポーネントイベントをディスパッチするために使用できるイベントディスパッチャーを作成します。イベントディスパッチャーは、namedetail の 2 つの引数を取ることができる関数です。

createEventDispatcher で作成されたコンポーネントイベントは、CustomEvent を作成します。これらのイベントは、バブリングしません。detail 引数は、CustomEvent.detail プロパティに対応し、任意の型のデータを含めることができます。

イベントディスパッチャーは、許可されたイベント名と detail 引数の型を絞り込むために型付けできます

const const dispatch: anydispatch = createEventDispatcher<{
 loaded: neverloaded: never; // does not take a detail argument
 change: stringchange: string; // takes a detail argument of type string, which is required
 optional: number | nulloptional: number | null; // takes an optional detail argument of type number
}>();
function createEventDispatcher<
	EventMap extends Record<string, any> = any
>(): EventDispatcher<EventMap>;

createRawSnippet

プログラムでスニペットを作成します

function createRawSnippet<Params extends unknown[]>(
	fn: (...params: Getters<Params>) => {
		render: () => string;
		setup?: (element: Element) => void | (() => void);
	}
): Snippet<Params>;

flushSync

保留中の状態変更と、それに起因する状態変更を同期的にフラッシュします。

function flushSync(fn?: (() => void) | undefined): void;

getAllContexts

最も近い親コンポーネントに属するコンテキストマップ全体を取得します。コンポーネントの初期化中に呼び出す必要があります。たとえば、プログラムでコンポーネントを作成し、既存のコンテキストを渡したい場合に役立ちます。

function getAllContexts<
	T extends Map<any, any> = Map<any, any>
>(): T;

getContext

指定された key を持つ、最も近い親コンポーネントに属するコンテキストを取得します。コンポーネントの初期化中に呼び出す必要があります。

function getContext<T>(key: any): T;

hasContext

親コンポーネントのコンテキストに、指定された key が設定されているかどうかを確認します。コンポーネントの初期化中に呼び出す必要があります。

function hasContext(key: any): boolean;

hydrate

指定されたターゲットにコンポーネントをハイドレートし、コンポーネントのエクスポートと、場合によってはプロパティ (accessors: true でコンパイルされた場合) を返します

function hydrate<
	Props extends Record<string, any>,
	Exports extends Record<string, any>
>(
	component:
		| ComponentType<SvelteComponent<Props>>
		| Component<Props, Exports, any>,
	options: {} extends Props
		? {
				target: Document | Element | ShadowRoot;
				props?: Props;
				events?: Record<string, (e: any) => any>;
				context?: Map<any, any>;
				intro?: boolean;
				recover?: boolean;
			}
		: {
				target: Document | Element | ShadowRoot;
				props: Props;
				events?: Record<string, (e: any) => any>;
				context?: Map<any, any>;
				intro?: boolean;
				recover?: boolean;
			}
): Exports;

mount

指定されたターゲットにコンポーネントをマウントし、コンポーネントのエクスポートと、場合によってはプロパティ (accessors: true でコンパイルされた場合) を返します。intro オプションが false に設定されていない限り、初期レンダリング中にトランジションが再生されます。

function mount<
	Props extends Record<string, any>,
	Exports extends Record<string, any>
>(
	component:
		| ComponentType<SvelteComponent<Props>>
		| Component<Props, Exports, any>,
	options: MountOptions<Props>
): Exports;

onDestroy

コンポーネントがアンマウントされる直前に実行されるコールバックをスケジュールします。

onMountbeforeUpdateafterUpdate、および onDestroy のうち、これはサーバーサイドコンポーネント内で実行される唯一のものです。

function onDestroy(fn: () => any): void;

onMount

onMount 関数は、コンポーネントが DOM にマウントされた直後に実行されるコールバックをスケジュールします。コンポーネントの初期化中に呼び出す必要があります (ただし、コンポーネントに存在する必要はありません。外部モジュールから呼び出すことができます)。

onMount から関数が同期的に返された場合、コンポーネントがアンマウントされるときに呼び出されます。

onMountサーバーサイドコンポーネント内では実行されません。

function onMount<T>(
	fn: () =>
		| NotFunction<T>
		| Promise<NotFunction<T>>
		| (() => any)
): void;

setContext

任意の context オブジェクトを現在のコンポーネントと指定された key に関連付け、そのオブジェクトを返します。コンテキストは、getContext を使用してコンポーネントの子 (スロット付きコンテンツを含む) で利用できるようになります。

ライフサイクル関数と同様に、これはコンポーネントの初期化中に呼び出す必要があります。

function setContext<T>(key: any, context: T): T;

tick

保留中の状態変更がすべて適用されたら解決される promise を返します。

function tick(): Promise<void>;

unmount

以前に mount または hydrate を使用してマウントされたコンポーネントをアンマウントします。

function unmount(component: Record<string, any>): void;

untrack

$derived または $effect の内部で使用すると、fn の内部で読み取られた状態は依存関係として扱われません。

function $effect(fn: () => void | (() => void)): void
namespace $effect

Runs code when a component is mounted to the DOM, and then whenever its dependencies change, i.e. $state or $derived values. The timing of the execution is after the DOM has been updated.

Example:

$effect(() => console.log('The count is now ' + count));

If you return a function from the effect, it will be called right before the effect is run again, or when the component is unmounted.

Does not run during server side rendering.

https://svelte.dokyumento.jp/docs/svelte/$effect

@paramfn The function to execute
$effect
(() => {
// this will run when `data` changes, but not when `time` changes save(data, { timestamp: anytimestamp: untrack(() => time) }); });
function untrack<T>(fn: () => T): T;

Component

強力に型付けされた Svelte コンポーネントを作成するために使用できます。

例:

npm に component-library というコンポーネントライブラリがあり、そこから MyComponent というコンポーネントをエクスポートします。Svelte+TypeScript ユーザーのために、型定義を提供したいと考えています。したがって、index.d.ts を作成します

import type { interface Component<Props extends Record<string, any> = {}, Exports extends Record<string, any> = {}, Bindings extends keyof Props | "" = string>

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:

import type { Component } from 'svelte';
export declare const MyComponent: Component&#x3C;{ 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:

&#x3C;script lang="ts">
	import { MyComponent } from "component-library";
&#x3C;/script>
&#x3C;MyComponent foo={'bar'} />
Component
} from 'svelte';
export declare const
const MyComponent: Component<{
    foo: string;
}, {}, string>
MyComponent
: interface Component<Props extends Record<string, any> = {}, Exports extends Record<string, any> = {}, Bindings extends keyof Props | "" = string>

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:

import type { Component } from 'svelte';
export declare const MyComponent: Component&#x3C;{ 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:

&#x3C;script lang="ts">
	import { MyComponent } from "component-library";
&#x3C;/script>
&#x3C;MyComponent foo={'bar'} />
Component
<{ foo: stringfoo: string }> {}

これを型付けすると、Svelte 拡張機能を備えた VS Code のような IDE が intellisense を提供し、TypeScript を使用して Svelte ファイルでこのようにコンポーネントを使用できるようになります

<script lang="ts">
	import { MyComponent } from "component-library";
</script>
<MyComponent foo={'bar'} />
interface Component<
	Props extends Record<string, any> = {},
	Exports extends Record<string, any> = {},
	Bindings extends keyof Props | '' = string
> {}
(
	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 https://svelte.dokyumento.jp/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 https://svelte.dokyumento.jp/docs/svelte/v5-migration-guide#Components-are-no-longer-classes
	 * for more info.
	 */
	$set?(props: Partial<Props>): void;
} & Exports;
  • internal Svelte が使用する内部オブジェクト。使用または変更しないでください。
  • props コンポーネントに渡されたプロパティ。
element?: typeof HTMLElement;

コンポーネントのカスタム要素バージョン。customElement コンパイラオプションでコンパイルされた場合にのみ存在します

ComponentConstructorOptions

Svelte 4 では、コンポーネントはクラスです。Svelte 5 では、コンポーネントは関数です。コンポーネントをインスタンス化するには、代わりに mount を使用してください。詳細については、移行ガイドを参照してください。

interface ComponentConstructorOptions<
	Props extends Record<string, any> = Record<string, any>
> {}
target: Element | Document | ShadowRoot;
anchor?: Element;
props?: Props;
context?: Map<any, any>;
hydrate?: boolean;
intro?: boolean;
recover?: boolean;
sync?: boolean;
$$inline?: boolean;

ComponentEvents

新しい Component 型には、専用の Events 型はありません。代わりに ComponentProps を使用してください。

type ComponentEvents<Comp extends SvelteComponent> =
	Comp extends SvelteComponent<any, infer Events>
		? Events
		: never;

ComponentInternals

環境によって異なる内部実装の詳細

type ComponentInternals = Branded<{}, 'ComponentInternals'>;

ComponentProps

指定されたコンポーネントが予期するプロパティを取得するための便利な型。

例: 変数に MyComponent が予期するプロパティが含まれていることを確認します

import type { type ComponentProps<Comp extends SvelteComponent | Component<any, any>> = Comp extends SvelteComponent<infer Props extends Record<string, any>, any, any> ? Props : Comp extends Component<infer Props extends Record<...>, any, string> ? Props : never

Convenience type to get the props the given component expects.

Example: Ensure a variable contains the props expected by MyComponent:

import type { ComponentProps } from 'svelte';
import MyComponent from './MyComponent.svelte';

// Errors if these aren't the correct props expected by MyComponent.
const props: ComponentProps&#x3C;typeof MyComponent> = { foo: 'bar' };

In Svelte 4, you would do ComponentProps&#x3C;MyComponent> because MyComponent was a class.

Example: A generic function that accepts some component and infers the type of its props:

import type { Component, ComponentProps } from 'svelte';
import MyComponent from './MyComponent.svelte';

function withProps&#x3C;TComponent extends Component&#x3C;any>>(
	component: TComponent,
	props: ComponentProps&#x3C;TComponent>
) {};

// Errors if the second argument is not the correct props expected by the component in the first argument.
withProps(MyComponent, { foo: 'bar' });
ComponentProps
} from 'svelte';
import
type MyComponent = SvelteComponent<Record<string, any>, any, any>
const MyComponent: LegacyComponentType
MyComponent
from './MyComponent.svelte';
// Errors if these aren't the correct props expected by MyComponent. const const props: Record<string, any>props: type ComponentProps<Comp extends SvelteComponent | Component<any, any>> = Comp extends SvelteComponent<infer Props extends Record<string, any>, any, any> ? Props : Comp extends Component<infer Props extends Record<...>, any, string> ? Props : never

Convenience type to get the props the given component expects.

Example: Ensure a variable contains the props expected by MyComponent:

import type { ComponentProps } from 'svelte';
import MyComponent from './MyComponent.svelte';

// Errors if these aren't the correct props expected by MyComponent.
const props: ComponentProps&#x3C;typeof MyComponent> = { foo: 'bar' };

In Svelte 4, you would do ComponentProps&#x3C;MyComponent> because MyComponent was a class.

Example: A generic function that accepts some component and infers the type of its props:

import type { Component, ComponentProps } from 'svelte';
import MyComponent from './MyComponent.svelte';

function withProps&#x3C;TComponent extends Component&#x3C;any>>(
	component: TComponent,
	props: ComponentProps&#x3C;TComponent>
) {};

// Errors if the second argument is not the correct props expected by the component in the first argument.
withProps(MyComponent, { foo: 'bar' });
ComponentProps
<typeof const MyComponent: LegacyComponentTypeMyComponent> = { foo: stringfoo: 'bar' };

Svelte 4 では、MyComponent がクラスだったため、ComponentProps<MyComponent> を実行していました。

例: いくつかのコンポーネントを受け入れ、そのプロパティの型を推論する汎用関数

import type { interface Component<Props extends Record<string, any> = {}, Exports extends Record<string, any> = {}, Bindings extends keyof Props | "" = string>

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:

import type { Component } from 'svelte';
export declare const MyComponent: Component&#x3C;{ 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:

&#x3C;script lang="ts">
	import { MyComponent } from "component-library";
&#x3C;/script>
&#x3C;MyComponent foo={'bar'} />
Component
, type ComponentProps<Comp extends SvelteComponent | Component<any, any>> = Comp extends SvelteComponent<infer Props extends Record<string, any>, any, any> ? Props : Comp extends Component<infer Props extends Record<...>, any, string> ? Props : never

Convenience type to get the props the given component expects.

Example: Ensure a variable contains the props expected by MyComponent:

import type { ComponentProps } from 'svelte';
import MyComponent from './MyComponent.svelte';

// Errors if these aren't the correct props expected by MyComponent.
const props: ComponentProps&#x3C;typeof MyComponent> = { foo: 'bar' };

In Svelte 4, you would do ComponentProps&#x3C;MyComponent> because MyComponent was a class.

Example: A generic function that accepts some component and infers the type of its props:

import type { Component, ComponentProps } from 'svelte';
import MyComponent from './MyComponent.svelte';

function withProps&#x3C;TComponent extends Component&#x3C;any>>(
	component: TComponent,
	props: ComponentProps&#x3C;TComponent>
) {};

// Errors if the second argument is not the correct props expected by the component in the first argument.
withProps(MyComponent, { foo: 'bar' });
ComponentProps
} from 'svelte';
import
type MyComponent = SvelteComponent<Record<string, any>, any, any>
const MyComponent: LegacyComponentType
MyComponent
from './MyComponent.svelte';
function function withProps<TComponent extends Component<any>>(component: TComponent, props: ComponentProps<TComponent>): voidwithProps<function (type parameter) TComponent in withProps<TComponent extends Component<any>>(component: TComponent, props: ComponentProps<TComponent>): voidTComponent extends interface Component<Props extends Record<string, any> = {}, Exports extends Record<string, any> = {}, Bindings extends keyof Props | "" = string>

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:

import type { Component } from 'svelte';
export declare const MyComponent: Component&#x3C;{ 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:

&#x3C;script lang="ts">
	import { MyComponent } from "component-library";
&#x3C;/script>
&#x3C;MyComponent foo={'bar'} />
Component
<any>>(
component: TComponent extends Component<any>component: function (type parameter) TComponent in withProps<TComponent extends Component<any>>(component: TComponent, props: ComponentProps<TComponent>): voidTComponent, props: ComponentProps<TComponent>props: type ComponentProps<Comp extends SvelteComponent | Component<any, any>> = Comp extends SvelteComponent<infer Props extends Record<string, any>, any, any> ? Props : Comp extends Component<infer Props extends Record<...>, any, string> ? Props : never

Convenience type to get the props the given component expects.

Example: Ensure a variable contains the props expected by MyComponent:

import type { ComponentProps } from 'svelte';
import MyComponent from './MyComponent.svelte';

// Errors if these aren't the correct props expected by MyComponent.
const props: ComponentProps&#x3C;typeof MyComponent> = { foo: 'bar' };

In Svelte 4, you would do ComponentProps&#x3C;MyComponent> because MyComponent was a class.

Example: A generic function that accepts some component and infers the type of its props:

import type { Component, ComponentProps } from 'svelte';
import MyComponent from './MyComponent.svelte';

function withProps&#x3C;TComponent extends Component&#x3C;any>>(
	component: TComponent,
	props: ComponentProps&#x3C;TComponent>
) {};

// Errors if the second argument is not the correct props expected by the component in the first argument.
withProps(MyComponent, { foo: 'bar' });
ComponentProps
<function (type parameter) TComponent in withProps<TComponent extends Component<any>>(component: TComponent, props: ComponentProps<TComponent>): voidTComponent>
) {}; // Errors if the second argument is not the correct props expected by the component in the first argument. function withProps<LegacyComponentType>(component: LegacyComponentType, props: Record<string, any>): voidwithProps(const MyComponent: LegacyComponentTypeMyComponent, { foo: stringfoo: 'bar' });
type ComponentProps<
	Comp extends SvelteComponent | Component<any, any>
> =
	Comp extends SvelteComponent<infer Props>
		? Props
		: Comp extends Component<infer Props, any>
			? Props
			: never;

ComponentType

この型は、新しいComponent型を使用する場合には廃止されています。

type ComponentType<
	Comp extends SvelteComponent = SvelteComponent
> = (new (
	options: ComponentConstructorOptions<
		Comp extends SvelteComponent<infer Props>
			? Props
			: Record<string, any>
	>
) => Comp) & {
	/** The custom element version of the component. Only present if compiled with the `customElement` compiler option */
	element?: typeof HTMLElement;
};

EventDispatcher

interface EventDispatcher<
	EventMap extends Record<string, any>
> {}
<Type extends keyof EventMap>(
	...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;

MountOptions

mount()関数が受け付けるオプションを定義します。

type MountOptions<
	Props extends Record<string, any> = Record<string, any>
> = {
	/**
	 * 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<string, (e: any) => any>;
	/**
	 * Can be accessed via `getContext()` at the component level.
	 */
	context?: Map<any, any>;
	/**
	 * Whether or not to play transitions on initial render.
	 * @default true
	 */
	intro?: boolean;
} & ({} extends Props
	? {
			/**
			 * Component properties.
			 */
			props?: Props;
		}
	: {
			/**
			 * Component properties.
			 */
			props: Props;
		});

Snippet

#snippetブロックの型。 これを使用して、(例えば)コンポーネントが特定の型のスニペットを期待することを表現できます。

let { 
let banner: Snippet<[{
    text: string;
}]>
banner
}: {
banner: Snippet<[{
    text: string;
}]>
banner
: type Snippet = /*unresolved*/ anySnippet<[{ text: stringtext: string }]> } = function $props(): any

Declares the props that a component accepts. Example:

let { optionalProp = 42, requiredProp, bindableProp = $bindable() }: { optionalProp?: number; requiredProps: string; bindableProp: boolean } = $props();

https://svelte.dokyumento.jp/docs/svelte/$props

$props
();

スニペットは{@render ...}タグを通してのみ呼び出すことができます。

/docs/svelte/snippet

interface Snippet<Parameters extends unknown[] = []> {}
(
	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;

GitHubでこのページを編集