Skip to content

Cannot use runes with class fields using a Symbol identifier #17012

@dangodai

Description

@dangodai

Describe the bug

Disclaimer: I'm unsure if this is a bug or just unsupported, but the docs don't seem to mention it at all and the error message is not precise to the problem.

Class fields using a Symbol as their identifier/key cannot be assigned runes (state, derived, etc). See the reproduction below.

I suspect that the compiler should be transforming the Symbol field into a get/set pair backed by a private field as the docs claim to do for class fields. Doing this manually is a workaround in the meantime.

If that isn't possible then clarification in the docs and the error message would be appreciated.

Reproduction

Reproduction and example workaround: https://svelte.dev/playground/dd94e0351ecf4d28b26eb81098171351?version=5.41.2

In short, the follow code

class Foo {
	[Symbol()] = $state();
}

Results in the error

`$state(...)` 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.
https://svelte.dev/e/state_invalid_placement

Logs

System Info

System:
    OS: Linux 6.15 Arch Linux
    CPU: (12) x64 AMD Ryzen 5 2600 Six-Core Processor
    Memory: 6.37 GB / 15.54 GB
    Container: Yes
    Shell: 5.3.3 - /bin/bash
  Binaries:
    Node: 22.17.0 - /home/yung/.nvm/versions/node/v22.17.0/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 10.9.2 - /home/yung/.nvm/versions/node/v22.17.0/bin/npm
    pnpm: 10.13.1 - /home/yung/.local/share/pnpm/pnpm
  Browsers:
    Chromium: 139.0.7258.66
    Firefox: 141.0
    Firefox Developer Edition: 141.0
  npmPackages:
    svelte: ^5.39.9 => 5.39.9

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