-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Description
Describe the bug
$state()
can be used to define reactive class instance variables. This is generally very useful, though when writing a getter for public variables, there's a call stack error.
I later discovered this is called out in the docs:
The compiler transforms
done
andtext
intoget
/set
methods on the class prototype referencing private fields. This means the properties are not enumerable.
— https://svelte.dev/docs/svelte/$state#Classes
I see from the JS output that a compiled $.get(this.#myvar)
is necessary when returning the variable. Could the compiler detect when manually written getters are made, and perform this code transformation on our behalf?
Or instead, provide a warning when a get myvar() { return this.myvar }
is written by the author, and provide education on the private variable getter/setter transformation.
Reproduction
- Go to https://svelte.dev/playground/5eb453986f3f4615a312342b59bd297c?version=5.36.16
- Open
store.svelte.js
- Observe transformed store has 2 getters
System Info
Svelte 5.36.16
Severity
annoyance