-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Open
Labels
Milestone
Description
Describe the bug
This code throw Error when file named with .ts extention:
class ReactiveRecord {
store = $state(null);
}
export const create = () => {
return new ReactiveRecord();
}CompileError: $state(...) can only be used as a variable declaration initializer or a class field
If the file is named .js, everything is working correctly.
Reproduction
I've been researching the internal files and this is what I found:
svelte\src\compiler\phases\2-analyze:
function validate_call_expression(node, scope, path) {
//...
if (
rune === '$state' ||
rune === '$state.frozen' ||
rune === '$derived' ||
rune === '$derived.by'
) {
if (parent.type === 'VariableDeclarator') return;
if (parent.type === 'PropertyDefinition' && !parent.static && !parent.computed) return;
console.log("parent", parent);
e.state_invalid_placement(node, rune);
}
//...
}Logs
"parent": Node {
type: 'AssignmentExpression',
operator: '=',
left: Node {
type: 'MemberExpression',
object: Node {
type: 'ThisExpression',
},
property: Node {
type: 'Identifier',
name: 'store'
},
computed: false,
optional: false
},
right: Node {
type: 'CallExpression',
callee: Node {
name: '$state'
},
arguments: [ [Node] ],
optional: false
}
}
System Info
Node: 20.11.1
svelte: 5.0.0-next.132
ts: 5.4.5
tsconfig: auto, included in create@svelte without custom editions
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
Calling the Error for use with and without this preprocessorSeverity
annoyance
ilsubyeega