Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

deepmerge-ts

RebeccaStevens4mBSD-3-Clause7.1.3TypeScript support: included

Deeply merge 2 or more objects respecting type information.

merge, deepmerge, deep merge, deep-merge, inferred types, inferred-types, recursive merge, recursive-merge, ts, ts merge, ts-merge, typescript, typescript merge, typescript-merge

readme

DeepmergeTS

npm version jsr Version CI Coverage Status\ code style: prettier GitHub Discussions BSD 3 Clause license Commitizen friendly semantic-release

Deeply merge 2 or more objects respecting type information.

smart merge diagram

Any donations would be much appreciated. 😄

Enterprise Users

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 npm
npm install deepmerge-ts

# Install with pnpm
pnpm add deepmerge-ts

# Install with yarn
yarn add deepmerge-ts

# Install with bun
bun add deepmerge-ts

jsr

# Install in a node project
npx jsr add @rebeccastevens/deepmerge

# Install in a deno project
deno add jsr:@rebeccastevens/deepmerge

# Install in a bun project
bunx 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.
  • Customized merging.

Usage

Example using default config

import { deepmerge } from "deepmerge-ts";

const x = {
  record: {
    prop1: "value1",
    prop2: "value2",
  },
  array: [1, 2, 3],
  set: new Set([1, 2, 3]),
  map: new Map([
    ["key1", "value1"],
    ["key2", "value2"],
  ]),
};

const y = {
  record: {
    prop1: "changed",
    prop3: "value3",
  },
  array: [2, 3, 4],
  set: new Set([2, 3, 4]),
  map: new Map([
    ["key2", "changed"],
    ["key3", "value3"],
  ]),
};

const z = {
  record: {
    prop1: undefined,
    prop3: undefined,
    prop2: undefined,
    prop4: undefined,
  },
  array: undefined,
  set: undefined,
  map: undefined,
};

const merged = deepmerge(x, y, z);

console.log(merged);

// Prettierfied output:
//
// Object {
//   "record": Object {
//     "prop1": "changed",
//     "prop2": "value2",
//     "prop3": "value3",
//     "prop4": undefined,
//   },
//   "array": Array [1, 2, 3, 2, 3, 4],
//   "set": Set { 1, 2, 3, 4 },
//   "map": Map {
//     "key1" => "value1",
//     "key2" => "changed",
//     "key3" => "value3",
//   },
// }

You can try out this example at codesandbox.io.

Merging into a Target

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).

Customized 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.

See deepmerge custom docs for more details.

Performance

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.

classic merge animation

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.

smart merge animation

API

See API docs.

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

7.1.3 (2024-10-08)

7.1.2 (2024-10-08)

Performance Improvements

7.1.1 (2024-10-04)

Performance Improvements

  • add explicit return type to speed up types (8e1ff6d)

7.1.0 (2024-07-20)

Features

7.0.3 (2024-06-06)

Bug Fixes

7.0.2 (2024-06-03)

Bug Fixes

  • support older module resolutions (063675e), closes #480

7.0.1 (2024-05-21)

Bug Fixes

7.0.0 (2024-05-20)

Code Refactoring

  • rename DeepMergeFunctionUtils to DeepMergeUtils (e821255)
  • rename DeepMergeMerge* to DeepMerge* (fd4d2d4)

Features

  • allow filtering out values before merging them (0784f63), closes #460

BREAKING CHANGES

  • rename DeepMergeFunctionUtils to DeepMergeUtils
  • rename DeepMergeMerge* to DeepMerge*
  • allow filtering out values before merging them

6.0.3 (2024-05-20)

Bug Fixes

  • deepmergeInto unsafe key value assignment (6b04863)

6.0.2 (2024-05-20)

Bug Fixes

  • return type when using empty records (6b4ff3f), closes #465

6.0.1 (2024-05-20)

Bug Fixes

  • type when merging index signatures (5e8b9b6), closes #459

6.0.0 (2024-05-19)

Bug Fixes

  • type when merging optional properties of a record (fa9ace2)

Build System

  • remove typing support for typescript<4.7 (f2f5956)

Features

  • allow restricting what types can be passed in as parameters (69e9ba3), closes #305
  • remove deprecated type DeepMergeLeafHKT (1982e56)
  • undefined will no longer replace defined values by default (9c86605)

Performance Improvements

  • remove BlacklistedRecordProps (19d4944)

BREAKING CHANGES

  • remove deprecated type DeepMergeLeafHKT
  • undefined will no longer replace defined values by default
  • The order of the generics of deepmergeCustom and deepmergeIntoCustom have changed. If you are passing generics to these functions you need to update them.
  • remove typing support for typescript<4.7

5.1.0 (2023-04-04)

Features

  • expose some of the internal utils (a11a03d)

5.0.0 (2023-03-18)

Bug Fixes

chore

  • drop support for node 12 and 14 (77016f7)

BREAKING CHANGES

  • drop support for node 12 and 14

5.0.0-next.4 (2023-03-18)

Bug Fixes

5.0.0-next.3 (2023-03-18)

Bug Fixes

5.0.0-next.2 (2023-02-06)

Features

  • create deepmergeInto function (9c350a0), closes #51

5.0.0-next.1 (2023-02-02)

chore

  • drop support for node 12 and 14 (77016f7)

BREAKING CHANGES

  • drop support for node 12 and 14

    4.3.0 (2023-02-06)

Features

  • create deepmergeInto function (9c350a0), closes #51

4.2.2 (2022-09-19)

4.2.1 (2022-06-15)

Bug Fixes

4.2.0 (2022-06-15)

Features

  • export types for module resolution node 16 (20241c5)

Performance Improvements

  • ts: add variance annotations (cecc9db)
  • ts: use extends constraints on infer (f053e76)

4.1.0 (2022-06-13)

Features

  • treat module imports as records (20c0dfb), closes #133

4.0.4 (2022-06-13)

Bug Fixes

  • drop keys that have no enumerable properties (3363570)

4.0.3 (2022-04-06)

Bug Fixes

  • use explict return types for function that return a HKT (eb4183e), closes #94

4.0.2 (2022-03-31)

Bug Fixes

  • protect against prototype pollution (d637db7)

4.0.1 (2022-03-14)

Bug Fixes

  • deno: fix broken import in deno dist files (#85) (86faf2a)

4.0.0 (2022-02-26)

Bug Fixes

Features

  • allow for default merging via a special return value (658d1fd)
  • allow for implicit default merging (1d5e617)
  • allow for skipping properties completely via a special return value (#64) (676f2f6)

BREAKING CHANGES

  • MetaMetaData now must extends DeepMergeBuiltInMetaData

3.0.1 (2022-02-22)

Bug Fixes

  • allows readonly records in DeepMergeRecordsDefaultHKTInternalPropValueHelper (#60) (fc85dfa)

3.0.0 (2022-02-19)

Code Refactoring

  • unrequire unused types and values (c78e373)

Features

  • lone values will now be passed to mergeOthers rather than just returned (#57) (9c24584)
  • provide customizable meta data to custom merge functions (3d96692), closes #33

BREAKING CHANGES

  • some types have changed for deepmergeCustom

2.0.1 (2021-12-22)

2.0.0 (2021-11-22)

Bug Fixes

  • add better support for readonly types (#17) (ee59064)

BREAKING CHANGES

  • interface DeepMergeMergeFunctionURItoKind's signature has changed

1.1.7 (2021-11-22)

Bug Fixes

  • incorrect resulting type when merging 3+ readonly tuples (#20) (696a1b2)

1.1.6 (2021-11-22)

Performance Improvements

  • convert recursive types to tail-recursive versions (#15) (4401ac2)

1.1.5 (2021-10-18)

Bug Fixes

  • deno: deno release fixup (4b8ca98)

1.1.4 (2021-10-18)

1.1.3 (2021-09-21)

Bug Fixes

1.1.2 (2021-09-17)

Bug Fixes

  • current and legacy types trying to using the same file resulting in one being overridden (#10) (a5f334b)

Performance Improvements

  • add early escapes to loos when merging unknown types (17a92e1)
  • directly request enumerable keys so that they don't need to then be filtered (04a2a5f)
  • use imperative loops when building merged result of records (b36f7bc)

1.1.1 (2021-09-16)

Bug Fixes

  • add legacy type information (#6) (c7e1019)
  • only merge enumerable properties (#8) (0967070)

1.1.0 (2021-09-13)

Features

  • add support for custom merging (#4) (5413b81)

1.0.1 (2021-08-25)

Bug Fixes

  • husky install no longer runs on postinstall (7102229)

1.0.0 (2021-08-25)

Features

  • add "module" property to package.json (168747d)
  • add basic functionality (8e3ba66)