Skip to content
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

Closed
Ocean-OS opened this issue Dec 28, 2024 · 3 comments
Closed

Comments

@Ocean-OS
Copy link
Contributor

Ocean-OS commented Dec 28, 2024

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:

<script>
let count = $state(0);
let counter = () => count;
counter[Symbol.toPrimitive] = () => count;
</script>
<button onclick={()=>count++}>Count is {counter}</button>

In this example, counter uses Symbol.toPrimitive in an (admittedly dumb) attempt to add some reactivity without requiring a (visible) function call.
However, the code that interpolates counter compiles to:

button.textContent = `Count is ${counter ?? ""}`;

...which is a static statement with no reactivity.
While I have found that wrapping the value in $state like so...

/*... insert stuff from example... */
let reactiveCounter = $state(counter);
/* replace `Count is {counter}` with `Count is {reactiveCounter}`... */

...makes everything work, this feels like more work than necessary, since count is still a "dependency" of counter (and in turn reactiveCounter).
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

N/A

Severity

annoyance

@Leonidaz
Copy link

Leonidaz commented Dec 28, 2024

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: $.template_effect(() => $.set_text(text, `Count is ${counter() ?? ""}`));

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.

@7nik
Copy link
Contributor

7nik commented Dec 29, 2024

This is intentional. Duplicate of #14803, #13525, #13054, #9860 + related #11012 and #10629

@trueadm
Copy link
Contributor

trueadm commented Dec 30, 2024

As mentioned above, this is by design. The compiler cannot statically determine something has toPrimitive or valueOf etc, so instead, we bail-out. Otherwise we'd have have to assume everything might be reactive.

@trueadm trueadm closed this as not planned Won't fix, can't repro, duplicate, stale Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants