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

feat: provide $state warnings for accidental equality #11610

Merged
merged 21 commits into from
May 14, 2024

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented May 14, 2024

This PR helps developers using $state understand how it can incorrectly be used when checking for equality of object properties compared to their original values.

We now monkey patch the array prototype in DEV and also compile user-land === and == etc to dev time checks, seeing if the proxied state value matches that of its non proxied value.

Addresses #11556, #11403 and #10120.

Copy link

changeset-bot bot commented May 14, 2024

🦋 Changeset detected

Latest commit: 5b0a275

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dummdidumm
Copy link
Member

Crafting a good warning is tough 😅 Proposal:

"Detected an equality check between a $state proxy and a non-$state-proxy object. This equality check will always fail because the proxy has a different object identity. Ensure both operands are of the same kind for accurate results."

Adjusted accordingly for (last)indexOf/includes.

@@ -22,4 +22,4 @@

## state_proxy_equality_mismatch

> Detected an equality check between a $state proxy and a non-$state-proxy object for %method%. This equality check will always fail because the proxy has a different object identity. To ensure both operands are of the same kind for accurate results, consider using `$state.is(a, b)`.
> Reactive `$state(...)` proxies and the values they proxy have different identities. Because of this, comparisons with `%operator%` will produce unexpected results. Consider using `$state.is(a, b)` instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked the previous message a bit better because it more clearly told me that there's something wrong. What's there now is a more general explanation of the situation, where it sounds like I should use $state.is in more places than I need to in reality.

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

Successfully merging this pull request may close these issues.

None yet

3 participants