Skip to content

Commit 043f108

Browse files
sync svelte docs
1 parent 04a3645 commit 043f108

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

apps/svelte.dev/content/docs/svelte/02-runes/02-$state.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,15 @@ todos[0].done = !todos[0].done;
6868

6969
### Classes
7070

71-
You can also use `$state` in class fields (whether public or private):
71+
You can also use `$state` in class fields (whether public or private), or as the first assignment to a property immediately inside the `constructor`:
7272

7373
```js
7474
// @errors: 7006 2554
7575
class Todo {
7676
done = $state(false);
77-
text = $state();
7877

7978
constructor(text) {
80-
this.text = text;
79+
this.text = $state(text);
8180
}
8281

8382
reset() {
@@ -111,10 +110,9 @@ You can either use an inline function...
111110
// @errors: 7006 2554
112111
class Todo {
113112
done = $state(false);
114-
text = $state();
115113

116114
constructor(text) {
117-
this.text = text;
115+
this.text = $state(text);
118116
}
119117

120118
+++reset = () => {+++

apps/svelte.dev/content/docs/svelte/98-reference/.generated/compile-errors.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,38 @@ Cannot reassign or bind to snippet parameter
846846
This snippet is shadowing the prop `%prop%` with the same name
847847
```
848848

849+
### state_field_duplicate
850+
851+
```
852+
`%name%` has already been declared on this class
853+
```
854+
855+
An assignment to a class field that uses a `$state` or `$derived` rune is considered a _state field declaration_. The declaration can happen in the class body...
856+
857+
```js
858+
class Counter {
859+
count = $state(0);
860+
}
861+
```
862+
863+
...or inside the constructor...
864+
865+
```js
866+
class Counter {
867+
constructor() {
868+
this.count = $state(0);
869+
}
870+
}
871+
```
872+
873+
...but it can only happen once.
874+
875+
### state_field_invalid_assignment
876+
877+
```
878+
Cannot assign to a state field before its declaration
879+
```
880+
849881
### state_invalid_export
850882

851883
```
@@ -855,7 +887,7 @@ Cannot export state from a module if it is reassigned. Either export a function
855887
### state_invalid_placement
856888

857889
```
858-
`%rune%(...)` can only be used as a variable declaration initializer or a class field
890+
`%rune%(...)` can only be used as a variable declaration initializer, a class field declaration, or the first assignment to a class field at the top level of the constructor.
859891
```
860892

861893
### store_invalid_scoped_subscription

apps/svelte.dev/content/docs/svelte/98-reference/30-compiler-errors.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,38 @@ Cannot reassign or bind to snippet parameter
851851
This snippet is shadowing the prop `%prop%` with the same name
852852
```
853853

854+
### state_field_duplicate
855+
856+
```
857+
`%name%` has already been declared on this class
858+
```
859+
860+
An assignment to a class field that uses a `$state` or `$derived` rune is considered a _state field declaration_. The declaration can happen in the class body...
861+
862+
```js
863+
class Counter {
864+
count = $state(0);
865+
}
866+
```
867+
868+
...or inside the constructor...
869+
870+
```js
871+
class Counter {
872+
constructor() {
873+
this.count = $state(0);
874+
}
875+
}
876+
```
877+
878+
...but it can only happen once.
879+
880+
### state_field_invalid_assignment
881+
882+
```
883+
Cannot assign to a state field before its declaration
884+
```
885+
854886
### state_invalid_export
855887

856888
```
@@ -860,7 +892,7 @@ Cannot export state from a module if it is reassigned. Either export a function
860892
### state_invalid_placement
861893

862894
```
863-
`%rune%(...)` can only be used as a variable declaration initializer or a class field
895+
`%rune%(...)` can only be used as a variable declaration initializer, a class field declaration, or the first assignment to a class field at the top level of the constructor.
864896
```
865897

866898
### store_invalid_scoped_subscription

0 commit comments

Comments
 (0)