You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sites/svelte-5-preview/src/routes/docs/content/01-api/03-snippets.md
+30-33Lines changed: 30 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -219,48 +219,45 @@ Any content inside the component tags that is _not_ a snippet declaration implic
219
219
220
220
> 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
221
221
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
-
228
222
## Typing snippets
229
223
230
-
You can import the `Snippet`type from `'svelte'`:
224
+
Snippets implement the `Snippet`interface imported from `'svelte'`:
231
225
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
+
+ }>();
236
237
</script>
237
238
```
238
239
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.
240
241
241
-
```ts
242
-
// @errors: 2305
243
-
importtype { Snippet } from'svelte';
242
+
We can tighten things up further by declaring a generic, so that `data` and `row` refer to the same type:
244
243
245
-
typeSnippetWithNoArgs=Snippet;
246
-
typeSnippetWithOneArg=Snippet<[argOne: number]>;
247
-
typeSnippetWithMultipleArgs=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';
251
248
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
+
```
253
258
254
-
```svelte
255
-
{#snippet withNoArgs()}
256
-
<!-- -->
257
-
{/snippet}
259
+
## Snippets and slots
258
260
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.
0 commit comments