Skip to content
Source
Sign inSign up
TanStack/form

fix(form-core): support value types with a custom equals() method in deep equality #2261

OpenFrancoKaddour(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/2261/headopened

Showing 3 changed files with 60 additions and 0 deletions.

x
0 of 3 viewed, 3 left
5 additions and 0 deletions.changeset/evaluate-custom-equals.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/form-core': patch
+---
+​
+Support value types that expose a custom `equals()` method (Temporal types, Luxon `DateTime`, etc.) in the deep-equality check. Previously two equal instances were reported as unequal because they have no own enumerable keys, so a field backed by such a value stayed dirty even after being reset to its original value (#2195).
18 additions and 0 deletionspackages/form-core/src/utils.tsModified
View file
@@ -514,6 +514,24 @@
return true
​
+ // Delegate to a custom value-equality method (Temporal types, Luxon DateTime,
+ // Immutable collections, etc.) when both operands share a constructor that
+ // exposes one. This must run before the no-enumerable-keys guard below, which
+ // would otherwise treat every such value as unequal since they expose their
+ // state through getters/private fields rather than own enumerable keys (#2195).
+ // Both operands must expose the method: plain object literals also share the
+ // `Object` constructor, so a one-sided `equals` (e.g. `{ equals: () => true }`
+ // vs `{}`) must fall through to structural comparison instead of delegating.
+ if (
+ objA.constructor === objB.constructor &&
+ typeof (objA as { equals?: unknown }).equals === 'function' &&
+ typeof (objB as { equals?: unknown }).equals === 'function'
+ ) {