Skip to content

Conversation

@Seol-JY
Copy link

@Seol-JY Seol-JY commented Nov 21, 2025

Summary

Fixes #1394

merge and mergeWith now handle top-level type mismatches consistently with nested property behavior.

Before

merge(["1"], {a: 2})             // [ '1', a: 2 ]
merge({x: ["1"]}, {x: {a: 2}}).x // { a: 2 }
// Inconsistent behavior

After

merge(["1"], {a: 2})             // { a: 2 }
merge({x: ["1"]}, {x: {a: 2}}).x // { a: 2 }
// Consistent behavior - source type wins

Changes

Copilot AI review requested due to automatic review settings November 21, 2025 18:34
@vercel
Copy link

vercel bot commented Nov 21, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
es-toolkit Ready Ready Preview Comment Nov 21, 2025 6:52pm

Copilot finished reviewing on behalf of Seol-JY November 21, 2025 18:37
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes inconsistent behavior in merge and mergeWith functions when handling type mismatches between target and source arguments. Previously, top-level type mismatches (e.g., array target with object source) behaved differently than nested property type mismatches. The fix ensures that in both cases, the source type takes precedence.

Key changes:

  • Added top-level type checking in both merge and mergeWith to detect array/object mismatches before iteration
  • When types mismatch, creates a fresh target of the source's type and recursively merges
  • Added test cases demonstrating the consistent behavior between top-level and nested type mismatches

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/object/merge.ts Added top-level type mismatch detection (lines 48-56) that creates fresh target when types don't match
src/object/mergeWith.ts Added identical top-level type mismatch detection (lines 57-65) consistent with merge.ts
src/object/merge.spec.ts Added test verifying top-level type mismatch behavior matches nested behavior
src/object/mergeWith.spec.ts Added test verifying top-level type mismatch behavior matches nested behavior for mergeWith

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

merge and mergeWith treat properties inconsistently with top-level arguments

1 participant