-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat: provide $state warnings for accidental equality #11610
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
Conversation
🦋 Changeset detectedLatest commit: 5b0a275 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
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. |
packages/svelte/src/internal/client/dom/elements/bindings/select.js
Outdated
Show resolved
Hide resolved
@@ -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 |
There was a problem hiding this comment.
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.
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.