diff --git a/documentation/docs/07-misc/03-typescript.md b/documentation/docs/07-misc/03-typescript.md index 525ba51d2f..a85032d04e 100644 --- a/documentation/docs/07-misc/03-typescript.md +++ b/documentation/docs/07-misc/03-typescript.md @@ -195,7 +195,13 @@ Using it together with dynamic components to restrict what kinds of component ca ``` -Closely related to the `Component` type is the `ComponentProps` type which extracts the properties a component expects. +> [!LEGACY] In Svelte 4, components were of type `SvelteComponent` + +## Component helper types + +### ComponentProps + +`ComponentProps` extracts the properties a component expects. ```ts import type { Component, ComponentProps } from 'svelte'; @@ -211,6 +217,21 @@ function withProps>( withProps(MyComponent, { foo: 'bar' }); ``` +### ComponentExports + +`ComponentExports` extracts the exports of a component, in other words you can use it to declare its instance type: + +```svelte + + + +``` + ## Enhancing built-in DOM types Svelte provides a best effort of all the HTML DOM types that exist. Sometimes you may want to use experimental attributes or custom events coming from an action. In these cases, TypeScript will throw a type error, saying that it does not know these types. If it's a non-experimental standard attribute/event, this may very well be a missing typing from our [HTML typings](https://github.com/sveltejs/svelte/blob/main/packages/svelte/elements.d.ts). In that case, you are welcome to open an issue and/or a PR fixing it. diff --git a/documentation/docs/07-misc/07-v5-migration-guide.md b/documentation/docs/07-misc/07-v5-migration-guide.md index 1624755a6b..c7c2ea5de7 100644 --- a/documentation/docs/07-misc/07-v5-migration-guide.md +++ b/documentation/docs/07-misc/07-v5-migration-guide.md @@ -599,13 +599,14 @@ To declare that a component of a certain type is required: ```svelte @@ -613,6 +614,20 @@ To declare that a component of a certain type is required: ``` +To declare the instance type of a component, or in other words its exports: + +```svelte + + + +``` + The two utility types `ComponentEvents` and `ComponentType` are also deprecated. `ComponentEvents` is obsolete because events are defined as callback props now, and `ComponentType` is obsolete because the new `Component` type is the component type already (e.g. `ComponentType>` == `Component<{ prop: string }>`). ### bind:this changes diff --git a/packages/svelte/src/index.d.ts b/packages/svelte/src/index.d.ts index 262337ef0a..0a46e3daa9 100644 --- a/packages/svelte/src/index.d.ts +++ b/packages/svelte/src/index.d.ts @@ -241,7 +241,8 @@ export type ComponentProps> = * Convenience type to get the properties that given component exports. * * Example: Typing the `bind:this` for a component named `MyComponent` - * ``` + * + * ```ts *