Skip to content

Commit a952860

Browse files
docs: more docs on TS types (#14065)
* docs: more docs on TS types and a few related changes/enhancements closes #13940 * Apply suggestions from code review * adjust * Apply suggestions from code review * Update documentation/docs/07-misc/03-typescript.md * Update documentation/docs/07-misc/03-typescript.md * Update documentation/docs/07-misc/03-typescript.md --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 00f5b14 commit a952860

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ 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+
To extract the properties from a component, use `ComponentProps`.
199201

200202
```ts
201203
import type { Component, ComponentProps } from 'svelte';
@@ -211,6 +213,19 @@ function withProps<TComponent extends Component<any>>(
211213
withProps(MyComponent, { foo: 'bar' });
212214
```
213215

216+
To declare that a variable expects the constructor or instance type of a component:
217+
218+
```svelte
219+
<script lang="ts">
220+
import MyComponent from './MyComponent.svelte';
221+
222+
let componentConstructor: typeof MyComponent = MyComponent;
223+
let componentInstance: MyComponent;
224+
</script>
225+
226+
<MyComponent bind:this={componentInstance} />
227+
```
228+
214229
## Enhancing built-in DOM types
215230

216231
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,13 +599,14 @@ 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>

0 commit comments

Comments
 (0)