Skip to content

Commit afda88c

Browse files
committed
docs: more docs on TS types
and a few related changes/enhancements closes #13940
1 parent 4715dfa commit afda88c

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

documentation/docs/07-misc/03-typescript.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,13 @@ Using it together with dynamic components to restrict what kinds of component ca
195195
<DynamicComponent prop="foo" />
196196
```
197197

198-
Closely related to the `Component` type is the `ComponentProps` type which extracts the properties a component expects.
198+
> [!LEGACY] In Svelte 4, components were of type `SvelteComponent`
199+
200+
## Component helper types
201+
202+
### ComponentProps
203+
204+
`ComponentProps` extracts the properties a component expects.
199205

200206
```ts
201207
import type { Component, ComponentProps } from 'svelte';
@@ -211,6 +217,21 @@ function withProps<TComponent extends Component<any>>(
211217
withProps(MyComponent, { foo: 'bar' });
212218
```
213219

220+
### ComponentExports
221+
222+
`ComponentExports` extracts the exports of a component, in other words you can use it to declare its instance type:
223+
224+
```svelte
225+
<script lang="ts">
226+
import type { ComponentExports } from 'svelte';
227+
import { Component } from './Component.svelte';
228+
229+
let component: ComponentExports<typeof Component>;
230+
</script>
231+
232+
<Component bind:this={component} />
233+
```
234+
214235
## Enhancing built-in DOM types
215236

216237
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.

documentation/docs/07-misc/07-v5-migration-guide.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,20 +599,35 @@ To declare that a component of a certain type is required:
599599

600600
```svelte
601601
<script lang="ts">
602-
import type { Component } from 'svelte';
602+
import type { ---SvelteComponent--- +++Component+++ } from 'svelte';
603603
import {
604604
ComponentA,
605605
ComponentB
606606
} from 'component-library';
607607
608-
let component: Component<{ foo: string }> = $state(
608+
---let component: typeof SvelteComponent<{ foo: string }>---
609+
+++let component: Component<{ foo: string }>+++ = $state(
609610
Math.random() ? ComponentA : ComponentB
610611
);
611612
</script>
612613
613614
<svelte:component this={component} foo="bar" />
614615
```
615616

617+
To declare the instance type of a component, or in other words its exports:
618+
619+
```svelte
620+
<script lang="ts">
621+
+++import type { ComponentExports } from 'svelte';+++
622+
import { Component } from './Component.svelte';
623+
624+
---let component: Component;---
625+
+++let component: ComponentExports<typeof Component>;+++
626+
</script>
627+
628+
<Component bind:this={component} />
629+
```
630+
616631
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<SvelteComponent<{ prop: string }>>` == `Component<{ prop: string }>`).
617632

618633
### bind:this changes

packages/svelte/src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ export type ComponentProps<Comp extends SvelteComponent | Component<any, any>> =
241241
* Convenience type to get the properties that given component exports.
242242
*
243243
* Example: Typing the `bind:this` for a component named `MyComponent`
244-
* ```
244+
*
245+
* ```ts
245246
* <script lang="ts">
246247
* import MyComponent from '$lib/component';
247248
* let component: ComponentExports<typeof MyComponent> | undefined = undefined;

packages/svelte/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ declare module 'svelte' {
238238
* Convenience type to get the properties that given component exports.
239239
*
240240
* Example: Typing the `bind:this` for a component named `MyComponent`
241-
* ```
241+
*
242+
* ```ts
242243
* <script lang="ts">
243244
* import MyComponent from '$lib/component';
244245
* let component: ComponentExports<typeof MyComponent> | undefined = undefined;

0 commit comments

Comments
 (0)