Skip to content

$state(...) can only be used as a variable declaration initializer or a class field #12852

@mrcleanandfresh

Description

@mrcleanandfresh

Describe the bug

When I use any rune within a class in a file suffixed with .svelte.ts, I get compile errors while running vitest. However, I don't get compile errors while running pnpm dev or pnpm build. There may be something with vitest that doesn't know the new syntax; just a guess since this is experimental.

I am currently using runes in abstract and concrete classes.

I tried installing version 4.0.0-next.6 of the @sveltejs/vite-plugin-svelte, but that didn't change anything. I am currently on version 5.0.0-next.221 of Svelte. I am using Svelte Kit version 2.0.0

The Reproduction I created fails in both runtime and tests, but on my machine, it only fails while running tests. I have tried moving the state outside the abstract class, but it just moves the compile error to the concrete class.

I thought this was a Windows thing, so I tried WSL but got the same result. It's reasonable for me to be doing something wrong, too, so I thought I'd raise this issue in case.

versions

{
  "devDependencies": {
    "@changesets/cli": "^2.27.7",
    "@melt-ui/pp": "^0.3.2",
    "@melt-ui/svelte": "^0.83.0",
    "@playwright/test": "^1.28.1",
    "@sveltejs/adapter-auto": "^3.0.0",
    "@sveltejs/kit": "^2.0.0",
    "@sveltejs/vite-plugin-svelte": "^3.1.1",
    "@tailwindcss/typography": "^0.5.13",
    "@testing-library/jest-dom": "^6.4.8",
    "@testing-library/svelte": "^5.2.1",
    "@types/eslint": "^9.6.0",
    "@types/ms": "^0.7.34",
    "@vitest/coverage-istanbul": "^2.0.5",
    "autoprefixer": "^10.4.19",
    "daisyui": "^4.12.10",
    "eslint": "^9.0.0",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-svelte": "^2.36.0",
    "globals": "^15.0.0",
    "jsdom": "^24.1.1",
    "postcss": "^8.4.38",
    "prettier": "^3.1.1",
    "prettier-plugin-svelte": "^3.1.2",
    "prettier-plugin-tailwindcss": "^0.6.4",
    "svelte": "5.0.0-next.221",
    "svelte-check": "^3.6.0",
    "tailwindcss": "^3.4.4",
    "typescript": "^5.0.0",
    "typescript-eslint": "^8.0.0-alpha.20",
    "vite": "^5.0.3",
    "vitest": "^2.0.5"
  },
  "type": "module",
  "dependencies": {
    "@dintero/money": "^1.0.13",
    "@steeze-ui/heroicons": "^2.4.0",
    "@steeze-ui/svelte-icon": "^1.5.0",
    "@tailwindcss/forms": "^0.5.7",
    "changesets-gitlab": "^0.12.0",
    "date-fns": "^3.6.0",
    "dexie": "^4.0.8",
    "firebase": "^10.12.5",
    "firebase-admin": "^12.3.1",
    "lodash.get": "^4.4.2",
    "mathjs": "^13.0.3",
    "ms": "^2.1.3",
    "svelte-forms-lib": "^2.0.1",
    "vite-plugin-svgr": "^4.2.0",
    "vite-tsconfig-paths": "^4.3.2",
    "yup": "^1.4.0"
  }
}

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-ynxgvk?file=src%2Froutes%2F%2Bpage.svelte

Logs

❯ src/lib/foo.test.ts (0)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  src/lib/foo.test.ts [ src/lib/foo.test.ts ]
CompileError: src/lib/extendable.svelte.ts:3:24 `$state(...)` can only be used as a variable declaration initializer or a class field
 1 |  export class Extendable {
 2 |    constructor() {
 3 |      this._isSomeState = $state(false);
                                           ^
 4 |      this._hasSomething = $state(false);
 5 |    }
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed (1)
      Tests  no tests
   Start at  16:10:48
   Duration  0ms


 FAIL  Tests failed. Watching for file changes...
       press h to show help, press q to quit
Cancelling test run. Press CTRL+c again to exit forcefully.

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
    Memory: 35.31 GB / 63.92 GB
  Binaries:
    Node: 20.15.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.7.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.6.0 - C:\Program Files\nodejs\pnpm.CMD
    bun: 1.1.15 - ~\.bun\bin\bun.EXE
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    svelte: 5.0.0-next.221 => 5.0.0-next.221

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