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

Automatic class string coercion is not reactive #15289

Open
rChaoz opened this issue Feb 13, 2025 · 3 comments
Open

Automatic class string coercion is not reactive #15289

rChaoz opened this issue Feb 13, 2025 · 3 comments

Comments

@rChaoz
Copy link
Contributor

rChaoz commented Feb 13, 2025

Describe the bug

Considering this code:

class Test {
	thing = $state(0)
	toString() {
		return String(this.thing)
	}
}

const test = new Test()

Using {test} in the component template is not reactive (when thing changes), but {String(test)} is.

Reproduction

https://svelte.dev/playground/e44b1258b39c4477a38ccb9ecaa4cd21?version=5.20.0

annoyance

@rChaoz rChaoz changed the title State read inside toString() is not reactive Automatic class string coercion is not reactive Feb 13, 2025
@brunnerh
Copy link
Member

brunnerh commented Feb 13, 2025

I think this is a "won't fix" kind of issue; this has also been raised in regards to SvelteDate.
To not bloat the compiler output for every variable reference, you should explicitly call .toString().

@Conduitry
Copy link
Member

This has come up enough times now that it might make sense documenting this somewhere.

@Ocean-OS
Copy link
Contributor

Yeah, this reminds me of #14854, and from that issue, it appears that #14803, #13525, #13054, #9860 #11012 and #10629 are also related. I'm fairly certain that at least one issue every other month is more than enough to warrant some sort of documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants