-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Svelte incorrectly interprets values using Symbol.toPrimitive
as static
#14854
Comments
Svelte only compiles a variable to be included in template effects if the variable is a state / signal declared at the top level, object property or a function. Otherwise, if it seems like a primitive it will not be reactive. e.g. compiled code for a function: Since JavaScript is not a strongly typed language, it's really not possible to do a static code analysis to determine what actually functions return. SIDE NOTE: I've been mulling over an idea to introduce a rune for a signal hint for svelte. I know that the recommendation is to use object / class instances vs primitives but I think it will be still quite useful and eliminate a lot of questions / issues on why something is not reactive. And, would be really nice for composition and library authors. |
As mentioned above, this is by design. The compiler cannot statically determine something has |
Describe the bug
If a value in Svelte uses
Symbol.toPrimitive
for reactivity, Svelte doesn't see this in compilation and interprets the value as static. Here's an example:In this example,
counter
usesSymbol.toPrimitive
in an (admittedly dumb) attempt to add some reactivity without requiring a (visible) function call.However, the code that interpolates
counter
compiles to:...which is a static statement with no reactivity.
While I have found that wrapping the value in
$state
like so......makes everything work, this feels like more work than necessary, since
count
is still a "dependency" ofcounter
(and in turnreactiveCounter
).This is an edge case, but it would be useful if these values could be correctly interpreted as reactive by the compiler.
Reproduction
REPL
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: