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)

Most upvoted comments

The analysis that emit’s this warning is only for template, it doesn’t analyze effects.

And I know that template is also an effect, but the whole point is that this variable is not read there, only written.

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:this then it must be $state IMO 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 $state to a variable in runes mode and expect people to navigate around the gotchas – it should always be $state or it won’t work IMO.

@jrmajor It won’t update properly. Try putting the variable in an $effect or $effect.pre and then compare it to $state. See here.

Yeah if you read it in an effect it will not update but if you never read it from an effect and only use it in events for example it will work fine so the warning is not needed. But i think we should also add the logic to see if it’s used in an effect and warn if that’s the case (obviously you can’t always know for sure if behind a function there’s an effect but it’s a starter)

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 thing entirely and enforce that it’s always declared with $state otherwise you can’t bind to it.

There seems to be a different issue, though. If $state is used, the value is not cleared on unmount

It sees the value to null which is correctly happening. Maybe because you’re using typeof and that is an object on null too?