Skip to content

Can't initialize state in a Class #231

Closed
@AnzeKop

Description

@AnzeKop

The example for initialising state inside a class like described in the docs doesn't work and throws an error

[!] (plugin svelte) CompileError: $state(...) can only be used as a variable declaration initializer or a class field

export class Todo {
  done = $state(false);
  text = $state();

  constructor(text: string) {
    this.text = text;
  }

  reset() {
    this.text = "";
    this.done = false;
  }
}

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