Skip to content

Writing getter for public class stateful variables transforms into duplicate getters #16501

@theetrain

Description

@theetrain

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 and text into get / 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

  1. Go to https://svelte.dev/playground/5eb453986f3f4615a312342b59bd297c?version=5.36.16
  2. Open store.svelte.js
  3. Observe transformed store has 2 getters

System Info

Svelte 5.36.16

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions