Skip to content

fix(store): guard shallow comparison against mismatched collection types and support invalid dates - #375

Open
grzdev wants to merge 1 commit into
TanStack:mainfrom
grzdev:fix/shallow-type-and-date-comparisons
Open

grzdev wants to merge 1 commit into
TanStack:mainfrom
grzdev:fix/shallow-type-and-date-comparisons

Conversation

@grzdev

@grzdev grzdev commented Sep 29, 2026 •

Copy link
Copy Markdown

Summary

Fixes edge cases in the shallow equality helper:

  • Prevents false-positive equality when comparing distinct collection types (e.g. shallow(new Date(), {}), shallow(new Map(), {}), shallow(new Set(), {}), or shallow(new Map(), new Set()) previously evaluated to true because collection instances have zero own enumerable properties and fell through to empty object keys comparison).
  • Correctly treats invalid dates as equal using Object.is(objA.getTime(), objB.getTime()) (previously NaN !== NaN caused two invalid dates to be considered unequal).
  • Added comprehensive unit test suite in packages/store/tests/shallow.test.ts.
  • Added changeset declaring a patch release for @tanstack/store.

Summary by CodeRabbit

  • Bug Fixes
    • Shallow comparisons now return false when comparing a Map, Set, or Date with a different type.
    • Two invalid dates are now considered equal in shallow comparisons, while valid and invalid dates remain unequal.
    • Matching Maps and Sets continue to be compared by size and contents. Plain objects, arrays, and other values retain their existing comparison behavior.

@coderabbitai

coderabbitai Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 06da56b3-1091-4473-ab12-4cdd30f00f24

📥 Commits

Reviewing files that changed from the base of the PR and between a9cf232 and b880060.

📒 Files selected for processing (3)
  • .changeset/fix-shallow-mismatched-types-and-invalid-dates.md
  • packages/store/src/shallow.ts
  • packages/store/tests/shallow.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

The shallow comparison now returns false for mismatched Map, Set, or Date types. It compares Date timestamps with Object.is, so two invalid Dates compare equal. Tests cover these cases and other shallow comparisons. A patch changeset records the fixes.

Changes

Shallow comparison

Layer / File(s) Summary
Comparison behavior and coverage
packages/store/src/shallow.ts, packages/store/tests/shallow.test.ts, .changeset/fix-shallow-mismatched-types-and-invalid-dates.md
shallow checks for mismatched Map, Set, and Date types, and compares Date timestamps with Object.is. Tests cover these behaviors and primitive, reference, object, and array comparisons. A patch changeset records the fixes.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to b8800

This change fixes edge cases in the shallow comparison helper and adds tests for them. No merge-blocking risk was found.

Architecture Summary

Architecture risk: 🔵 Low · up to b8800

The change affects 1 system.

Changed systems: packages/store

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — packages/store (library) was modified; 2 changed files map to changed impact.

Before / after behavior

  • observed — Modified behavior in packages/store/src/shallow.ts: Map comparison now runs when either operand is a Map and returns false if only one is a Map; matching Maps retain the existing size and key/value checks.
  • observed — Modified behavior in packages/store/src/shallow.ts: Set comparison now runs when either operand is a Set and returns false if only one is a Set; matching Sets retain the existing size and membership checks.
  • observed — Modified behavior in packages/store/src/shallow.ts: Date comparison now runs when either operand is a Date and returns false if only one is a Date. Matching Dates compare timestamps with Object.is instead of !==, making two invalid Dates equal.
  • observed — Modified behavior in packages/store/tests/shallow.test.ts: Added tests asserting true for identical primitives (including NaN) and references, and false for differing primitives and nullish mismatches.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main changes to mismatched collection types and invalid date handling in the shallow comparison.
Description check ✅ Passed The description explains the changes, motivation, tests, and changeset. It does not use the template headings or confirm the checklist items, but it contains the main required information.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant