Skip to content

Commit 1ad7aef

Browse files
committed
docs: tweak "invalid assignment" compiler error message
fixes #14702
1 parent deb362f commit 1ad7aef

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

.changeset/sharp-dryers-try.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+
chore: tweak "invalid assignment" compiler error message

documentation/docs/98-reference/.generated/compile-errors.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,37 @@ The $ prefix is reserved, and cannot be used for variables and imports
331331
### each_item_invalid_assignment
332332

333333
```
334-
Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`)
334+
Cannot reassign or bind to the each block argument itself in runes mode. Use the array and index variables instead: For reassignments `array[i] = value` instead of `entry = value`, for bindings `bind:prop={array[i]}` instead of `bind:prop={value}`
335+
```
336+
337+
In legacy mode, it was possible to reassign or bind to the each block argument itself:
338+
339+
```svelte
340+
<script>
341+
let array = [1, 2, 3];
342+
</script>
343+
344+
{#each array as entry}
345+
<!-- reassignment -->
346+
<button on:click={() => entry = 4}>change</button>
347+
<!-- binding -->
348+
<input bind:value={entry}>
349+
{/each}
350+
```
351+
352+
This is no longer possible in runes mode, because the array could also be a computed value such as `{#each array.filter((i) => i % 2)}`, at which point things get unstable. Svelte therefore requires you to be explicit about what to bind to, requiring a stable reference. Therefore, use the array and index variables instead:
353+
354+
```svelte
355+
<script>
356+
let array = $state([1, 2, 3]);
357+
</script>
358+
359+
{#each array as entry, idx}
360+
<!-- reassignment -->
361+
<button onclick={() => array[idx] = 4}>change</button>
362+
<!-- binding -->
363+
<input bind:value={array[idx]}>
364+
{/each}
335365
```
336366

337367
### effect_invalid_placement

packages/svelte/messages/compile-errors/script.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,37 @@
3232
3333
## each_item_invalid_assignment
3434

35-
> Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`)
35+
> Cannot reassign or bind to the each block argument itself in runes mode. Use the array and index variables instead: For reassignments `array[i] = value` instead of `entry = value`, for bindings `bind:prop={array[i]}` instead of `bind:prop={value}`
36+
37+
In legacy mode, it was possible to reassign or bind to the each block argument itself:
38+
39+
```svelte
40+
<script>
41+
let array = [1, 2, 3];
42+
</script>
43+
44+
{#each array as entry}
45+
<!-- reassignment -->
46+
<button on:click={() => entry = 4}>change</button>
47+
<!-- binding -->
48+
<input bind:value={entry}>
49+
{/each}
50+
```
51+
52+
This is no longer possible in runes mode, because the array could also be a computed value such as `{#each array.filter((i) => i % 2)}`, at which point things get unstable. Svelte therefore requires you to be explicit about what to bind to, requiring a stable reference. Therefore, use the array and index variables instead:
53+
54+
```svelte
55+
<script>
56+
let array = $state([1, 2, 3]);
57+
</script>
58+
59+
{#each array as entry, idx}
60+
<!-- reassignment -->
61+
<button onclick={() => array[idx] = 4}>change</button>
62+
<!-- binding -->
63+
<input bind:value={array[idx]}>
64+
{/each}
65+
```
3666

3767
## effect_invalid_placement
3868

packages/svelte/src/compiler/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ export function dollar_prefix_invalid(node) {
151151
}
152152

153153
/**
154-
* Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`)
154+
* Cannot reassign or bind to the each block argument itself in runes mode. Use the array and index variables instead: For reassignments `array[i] = value` instead of `entry = value`, for bindings `bind:prop={array[i]}` instead of `bind:prop={value}`
155155
* @param {null | number | NodeLike} node
156156
* @returns {never}
157157
*/
158158
export function each_item_invalid_assignment(node) {
159-
e(node, "each_item_invalid_assignment", `Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. \`array[i] = value\` instead of \`entry = value\`)\nhttps://svelte.dev/e/each_item_invalid_assignment`);
159+
e(node, "each_item_invalid_assignment", `Cannot reassign or bind to the each block argument itself in runes mode. Use the array and index variables instead: For reassignments \`array[i] = value\` instead of \`entry = value\`, for bindings \`bind:prop={array[i]}\` instead of \`bind:prop={value}\`\nhttps://svelte.dev/e/each_item_invalid_assignment`);
160160
}
161161

162162
/**

0 commit comments

Comments
 (0)