svelte: False positive “updated, but not declared with `$state`” warning
Describe the bug
With the following code in runes mode:
<script>
let input;
</script>
{#if true}
<input bind:this={input}/>
<button onclick={() => input?.focus()}>focus input</button>
{/if}
I get the following warning: input is updated, but is not declared with $state(...). Changing its value will not correctly trigger updates (non_reactive_update).
This doesn’t happen unless the bound element is defined in a conditional.
Reproduction
Here’s above example in REPL.
Logs
No response
System Info
System:
OS: macOS 14.6.1
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 487.38 MB / 16.00 GB
Shell: 3.7.1 - /usr/local/bin/fish
Binaries:
Node: 22.7.0 - /usr/local/bin/node
Yarn: 1.22.22 - /usr/local/bin/yarn
npm: 10.8.2 - /usr/local/bin/npm
pnpm: 9.9.0 - /usr/local/bin/pnpm
bun: 1.1.26 - /usr/local/bin/bun
Browsers:
Safari: 17.6
npmPackages:
svelte: 5.0.0-next.242 => 5.0.0-next.242
Severity
annoyance
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 26 (26 by maintainers)
If it’s not read anywhere, then we should just remove the binding altogether as it’s completely not needed and thus overhead. If it is read somewhere, then it matters where it is read – and if it’s referenced anywhere but the
bind:thisthen it must be$stateIMO as there are far too many gotchas coming around from this now. Really, it’s just backwards that we allow you to bind a variable that isn’t$stateto a variable in runes mode and expect people to navigate around the gotchas – it should always be$stateor it won’t work IMO.It doesn’t work as a prop either. We can’t add logic to see if it’s used in an effect as we don’t know if it will be used – it could be passed around to a function that then uses it. Our best bet it is to warn if it’s used anywhere but a
bind:this.Honestly, I’m of the position that we should just disallow
let thingentirely and enforce that it’s always declared with$stateotherwise you can’t bind to it.It sees the value to
nullwhich is correctly happening. Maybe because you’re usingtypeofand that is an object onnulltoo?