Skip to content

Svelte 5: compiler may falsy consider variable as non-reactive #9860

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
7nik opened this issue Dec 8, 2023 · 5 comments
Closed

Svelte 5: compiler may falsy consider variable as non-reactive #9860

7nik opened this issue Dec 8, 2023 · 5 comments

Comments

@7nik
Copy link
Contributor

7nik commented Dec 8, 2023

Describe the bug

The compiler may consider the variable non-reactive if no explicit calls of methods. However, an object may have a custom stringification method that reads a signal. Such methods are toString, toJSON, valueOf, and Symbol.toPrimitive.

Reproduction

REPL

Logs

No response

System Info

REL Svelte v5.0.0-next.22

Severity

annoyance

@Conduitry
Copy link
Member

This one feels like a won't-fix (or maybe even a working-as-designed) to me. If supporting this requires any additional runtime code for people not using a custom toString(), I don't think we should support it.

@7nik
Copy link
Contributor Author

7nik commented Dec 8, 2023

The original case is more tricky, but I kinda agree.

@7nik
Copy link
Contributor Author

7nik commented Dec 10, 2023

What about this case with a Date? I suppose date isn't reactive because it is a Date class instance, but shouldn't date = date trigger reactivity? How can I trigger reactivity here without creating a new object?
Though, probably it is another issue.

@trueadm
Copy link
Contributor

trueadm commented Dec 11, 2023

@7nik That's because Svelte doesn't treat Date as reactive, so changing its internals will do nothing. You need to re-assign date to a new Date object each time if you want it to be reactive, there's no other way. However, you can rewrite your example so it's a lot cleaner:

@trueadm
Copy link
Contributor

trueadm commented Dec 12, 2023

Closing this as I think this isn't something we need to fix.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants