Skip to content

Commit 72fec99

Browse files
feat: improve type arguments for Snippet and $bindable (#12197)
* Improve type arguments for snippet and bindable * generate:types * tweak * changeset --------- Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 2c807ad commit 72fec99

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

.changeset/tricky-laws-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
feat: improve type arguments for Snippet and $bindable

packages/svelte/src/ambient.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ declare function $props(): any;
306306
*
307307
* https://svelte-5-preview.vercel.app/docs/runes#$bindable
308308
*/
309-
declare function $bindable<T>(t?: T): T;
309+
declare function $bindable<T>(fallback?: T): T;
310310

311311
/**
312312
* Inspects one or more values whenever they, or the properties they contain, change. Example:

packages/svelte/src/index.d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,20 +259,24 @@ declare const SnippetReturn: unique symbol;
259259
/**
260260
* The type of a `#snippet` block. You can use it to (for example) express that your component expects a snippet of a certain type:
261261
* ```ts
262-
* let { banner }: { banner: Snippet<{ text: string }> } = $props();
262+
* let { banner }: { banner: Snippet<[{ text: string }]> } = $props();
263263
* ```
264264
* You can only call a snippet through the `{@render ...}` tag.
265+
*
266+
* https://svelte-5-preview.vercel.app/docs/snippets
267+
*
268+
* @template Parameters the parameters that the snippet expects (if any) as a tuple.
265269
*/
266-
export type Snippet<T extends unknown[] = []> =
270+
export type Snippet<Parameters extends unknown[] = []> =
267271
// this conditional allows tuples but not arrays. Arrays would indicate a
268272
// rest parameter type, which is not supported. If rest parameters are added
269273
// in the future, the condition can be removed.
270-
number extends T['length']
274+
number extends Parameters['length']
271275
? never
272276
: {
273277
(
274278
this: void,
275-
...args: T
279+
...args: Parameters
276280
): typeof SnippetReturn & {
277281
_: 'functions passed to {@render ...} tags must use the `Snippet` type imported from "svelte"';
278282
};

packages/svelte/types/index.d.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,20 +256,24 @@ declare module 'svelte' {
256256
/**
257257
* The type of a `#snippet` block. You can use it to (for example) express that your component expects a snippet of a certain type:
258258
* ```ts
259-
* let { banner }: { banner: Snippet<{ text: string }> } = $props();
259+
* let { banner }: { banner: Snippet<[{ text: string }]> } = $props();
260260
* ```
261261
* You can only call a snippet through the `{@render ...}` tag.
262+
*
263+
* https://svelte-5-preview.vercel.app/docs/snippets
264+
*
265+
* @template Parameters the parameters that the snippet expects (if any) as a tuple.
262266
*/
263-
type Snippet<T extends unknown[] = []> =
267+
type Snippet<Parameters extends unknown[] = []> =
264268
// this conditional allows tuples but not arrays. Arrays would indicate a
265269
// rest parameter type, which is not supported. If rest parameters are added
266270
// in the future, the condition can be removed.
267-
number extends T['length']
271+
number extends Parameters['length']
268272
? never
269273
: {
270274
(
271275
this: void,
272-
...args: T
276+
...args: Parameters
273277
): typeof SnippetReturn & {
274278
_: 'functions passed to {@render ...} tags must use the `Snippet` type imported from "svelte"';
275279
};
@@ -3015,7 +3019,7 @@ declare function $props(): any;
30153019
*
30163020
* https://svelte-5-preview.vercel.app/docs/runes#$bindable
30173021
*/
3018-
declare function $bindable<T>(t?: T): T;
3022+
declare function $bindable<T>(fallback?: T): T;
30193023

30203024
/**
30213025
* Inspects one or more values whenever they, or the properties they contain, change. Example:

0 commit comments

Comments
 (0)