Open
Description
Describe the bug
I found big inconsistencies in when/if the state_unsafe_mutation
warning/error is shown.
This is the setup:
<script>
class State {
_x = $state(null)
get_x() {
if (this._x === null) {
this._x = 5
}
return this._x
}
get x() {
if (this._x === null) {
this._x = 5
}
return this._x
}
}
const s = new State()
// $inspect(s.get_x()) // makes a difference when uncommented
// $inspect(s.x) // makes a difference when uncommented
// $inspect(s._x) // does NOT make a difference when uncommented
</script>
<h1>Hello {s.get_x()}!</h1>
<h1>Hello {s.x}!</h1>
This results in the following error:
state_unsafe_mutation Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state` https://svelte.dev/e/state_unsafe_mutation in {expression} in App.svelte
However, if one of the top two $inspect
s is uncommented, the code passes and runs with the expected result.
If the bottom $inspect
is uncommented, the code fails and does not run, the above error is thrown.
Reproduction
https://svelte.dev/playground/686feb80ab0e41d29668232eacfdf8a4?version=5.34.2
Logs
System Info
playground
Severity
annoyance
Metadata
Metadata
Assignees
Labels
No labels