deepmerge-ts is available as part of the Tidelift Subscription.
Tidelift is working with the maintainers of deepmerge-ts and a growing network of open source maintainers to ensure
your open source software supply chain meets enterprise standards now and into the future.
Learn more.
Installation
npm
# Install with npmnpm install deepmerge-ts# Install with pnpmpnpm add deepmerge-ts# Install with yarnyarn add deepmerge-ts# Install with bunbun add deepmerge-ts
jsr
# Install in a node projectnpx jsr add @rebeccastevens/deepmerge# Install in a deno projectdeno add jsr:@rebeccastevens/deepmerge# Install in a bun projectbunx jsr add @rebeccastevens/deepmerge
Features
Smart merging - High performance.
Merged output has correct typing.
Record merging support.
Array merging support.
Map and Set merging support.
Circular reference support.
Security safeguards.
Recursion depth limit (defaults to 1000) to prevent stack exhaustion.
High-performance deepmergeFastUnsafe variants for trusted data.
You can use deepmergeInto if you want to update a target object with the merge result instead of creating a new
object.
This function is best used with objects that are all of the same type.
Note: If the target object's type differs from the input objects, we'll assert that the target's type has changed
(this is not done automatically with deepmergeIntoCustom).
Customizing the Merging Process
We provide a customizer function for each of our main deepmerge functions: deepmergeCustom and deepmergeIntoCustom.
You can use these to customize the details of how values should be merged together.
Due to how TypeScript handles interfaces (microsoft/TypeScript#15300),
interface types without an explicit index signature may not appear to merge correctly.
For proper type inference when merging objects, use type aliases instead of interfaces.
High-Performance "FastUnsafe" Variants
For performance-critical code where inputs are trusted and non-circular, we provide high-performance variants that skip
circular reference tracking, depth limits, prototype pollution safeguards, and metadata allocations:
[!WARNING]
Only use these functions with trusted, non-circular data. Using with untrusted user data can result in security
vulnerabilities such as prototype pollution and Denial of Service (DoS) from unbounded recursion / stack overflow.
We use smart merging instead of the classic merging strategy which some alternative libraries use. This vastly improves
performance, both in execution time and memory usage.
Classic Merge (not what we do)
With classic merging, each input is merged with the next input until all inputs are merged.
This strategy has large performance issues when lots of items need to be merged.
Smart Merge (what we do)
With our smart merging, we look ahead to see what can be merged and only merge those things.
In addition to performance improvements, this strategy merges multiple inputs at once; allowing for benefits such as
taking averages of the inputs.