You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Svelte 5, we can use reactive variables in classes.
You can also create 'reactive' files (example.svelte.js).
Combining these two ideas also works fine.
However, when I do the same thing in Typescript, it compiles into a broken state.
Reproduction
In file: +page.svelte:
<script> <!-- OR: <script lang="ts"> -->
import { test } from './test.svelte';
</script>
In file test.svelte.ts:
class Test {
something = $state()
}
export const test = new Test();
Note: It works fine doing the same with a JS file (text.svelte.js)
Logs
11:21:10 [vite] Internal server error: src/routes/test.svelte.ts:3:21 `$state(...)` can only be used as a variable declaration initializer or a class field
https://svelte.dev/e/state_invalid_placement
Plugin: vite-plugin-svelte-module
File: src/routes/test.svelte.ts:2:16
1 | class Test {
2 |constructor() {
3 | this.something = $state();
^
4 | }
5 | }
Describe the bug
In Svelte 5, we can use reactive variables in classes.
You can also create 'reactive' files (
example.svelte.js
).Combining these two ideas also works fine.
However, when I do the same thing in Typescript, it compiles into a broken state.
Reproduction
In file:
+page.svelte
:In file
test.svelte.ts
:Note: It works fine doing the same with a JS file (
text.svelte.js
)Logs
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: