Skip to content

Commit 0107dc3

Browse files
committed
update typing docs, so that it flows from previous example
1 parent 3198e20 commit 0107dc3

File tree

1 file changed

+30
-33
lines changed

1 file changed

+30
-33
lines changed

sites/svelte-5-preview/src/routes/docs/content/01-api/03-snippets.md

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -219,48 +219,45 @@ Any content inside the component tags that is _not_ a snippet declaration implic
219219

220220
> Note that you cannot have a prop called `children` if you also have content inside the component — for this reason, you should avoid having props with that name
221221
222-
## Snippets and slots
223-
224-
In Svelte 4, content can be passed to components using [slots](https://svelte.dev/docs/special-elements#slot). Snippets are more powerful and flexible, and as such slots are deprecated in Svelte 5.
225-
226-
They continue to work, however, and you can mix and match snippets and slots in your components.
227-
228222
## Typing snippets
229223

230-
You can import the `Snippet` type from `'svelte'`:
224+
Snippets implement the `Snippet` interface imported from `'svelte'`:
231225

232-
```svelte
233-
<script lang="ts">
234-
import type { Snippet } from 'svelte';
235-
let { header } = $props<{ header: Snippet }>();
226+
```diff
227+
-<script>
228+
- let { data, children, row } = $props();
229+
+<script lang="ts">
230+
+ import type { Snippet } from 'svelte';
231+
+
232+
+ let { data, children, row } = $props<{
233+
+ data: any[];
234+
+ children: Snippet;
235+
+ row: Snippet<[any]>;
236+
+ }>();
236237
</script>
237238
```
238239

239-
The `Snippet` type is generic. Here's how you'd type various cases:
240+
With this change, red squigglies will appear if you try and use the component without providing a `data` prop and a `row` snippet. Notice that the type argument provided to `Snippet` is a tuple, since snippets can have multiple parameters.
240241

241-
```ts
242-
// @errors: 2305
243-
import type { Snippet } from 'svelte';
242+
We can tighten things up further by declaring a generic, so that `data` and `row` refer to the same type:
244243

245-
type SnippetWithNoArgs = Snippet;
246-
type SnippetWithOneArg = Snippet<[argOne: number]>;
247-
type SnippetWithMultipleArgs = Snippet<
248-
[argOne: number, argTwo: string]
249-
>;
250-
```
244+
```diff
245+
-<script lang="ts">
246+
+<script lang="ts" generics="T">
247+
import type { Snippet } from 'svelte';
251248

252-
And here are the snippet declarations matching those cases (note: this example uses TypeScript):
249+
let { data, children, row } = $props<{
250+
- data: any[];
251+
+ data: T[];
252+
children: Snippet;
253+
- row: Snippet<[any]>;
254+
+ row: Snippet<[T]>;
255+
}>();
256+
</script>
257+
```
253258

254-
```svelte
255-
{#snippet withNoArgs()}
256-
<!-- -->
257-
{/snippet}
259+
## Snippets and slots
258260

259-
{#snippet withOneArg(argOne: number)}
260-
<!-- -->
261-
{/snippet}
261+
In Svelte 4, content can be passed to components using [slots](https://svelte.dev/docs/special-elements#slot). Snippets are more powerful and flexible, and as such slots are deprecated in Svelte 5.
262262

263-
{#snippet withMultipleArgs(argOne: number, argTwo: string)}
264-
<!-- -->
265-
{/snippet}
266-
```
263+
They continue to work, however, and you can mix and match snippets and slots in your components.

0 commit comments

Comments
 (0)