7.0.0 • Published 3 months ago

ts-deepmerge v7.0.0

Weekly downloads
2,526
License
ISC
Repository
github
Last release
3 months ago

npm

TypeScript Deep Merge

A deep merge function that automatically infers the return type based on your input, without mutating the source objects.

Objects and arrays will be merged, but values such as numbers and strings will be overwritten.

All merging/overwriting occurs in the order of the arguments you provide the function with.

Both ESM and CommonJS are supported by this package.

Usage

import { merge } from "ts-deepmerge";

const obj1 = {
  a: {
    a: 1
  }
};

const obj2 = {
  b: {
    a: 2,
    b: 2
  }
};

const obj3 = {
  a: {
    b: 3
  },
  b: {
    b: 3,
    c: 3
  },
  c: 3
};

const result = merge(obj1, obj2, obj3);

The value of the above result is:

{
  "a": {
    "a": 1,
    "b": 3
  },
  "b": {
    "a": 2,
    "b": 3,
    "c": 3
  },
  "c": 3
}

With options

If you would like to provide options to change the merge behaviour, you can use the .withOptions method:

import { merge } from "ts-deepmerge";

const obj1 = {
  array: ["A"],
};

const obj2 = {
  array: ["B"],
}

const result = merge.withOptions(
  { mergeArrays: false },
  obj1,
  obj2
);

The value of the above result is:

{
  "array": ["B"]
}

All options have JSDoc descriptions in its source.

When working with generic declared types/interfaces

There's currently a limitation with the inferred return type that ts-deepmerge offers, where it's unable to take the order of the objects/properties into consideration due to the nature of accepting an infinite number of objects to merge as args and what TypeScript currently offers to infer the types. The primary use case for the inferred return type is for basic object primitives, to offer something more useful as the return type, which does work for a lot of cases.

If you're working with generic declared types though, this can cause the inferred return type to not align with what you may expect, as it currently detects every possible value and combines them as a union type. When working with declared types, and you know what the final type will align to, simply use the as keyword as shown in the example below:

interface IObj {
  a: string;
  b: string;
}

const obj1: IObj = { a: "1", b: "2", };
const obj2: Partial<IObj> = { a: "1" };

const result = merge(obj1, obj2) as IObj;

More context can be found in this issue.

@tscmono/repo@markmctamney/eslint-config-markmctamney@volley/solid-gev-waterfall@infinitebrahmanuniverse/nolb-ts-daction-challenge@everything-registry/sub-chunk-2976testabulousgrimacegenshin-managerjson-schema-to-decodersdry-axiosecsactfaunaticfirebase-functions-testfm-react-firebaseopenapi-doc-parserpayload-plugin-schedulerpayload-plugin-search@dcl/inspectorkotini-librarykickstartdsnextloveplop-scaffoldplotmanminerendermineflayer-autocrystal-anarchymineflayer-mitm-proxyparispizzadoor-stocks-managersalutejsreact-svg-timeline-adrienvreact-svg-timelinereact-test-wrapperreact-textgame-enginereact-native-themed-stylesheetromcalsveltekit-superformssveltekit-templatetermicalcdktf-registry-docsstorybook-facelifttezos-pulumicdk8s-metaflowcreate-webstone-app@makina-corpus/rando3d@lokalise/background-jobs-common@mrpelz/boilerplate-common@proedis/react-app-client@proedis/utils@ngx-composer/cli@nexus-ui/tailwind-config@polyipseity/obsidian-plugin-library@mtvproject/inspector@mrroll/style-guide@mrroll/next-config@prerenderer/prerenderer@prerenderer/renderer-jsdom@prerenderer/renderer-puppeteer@prerenderer/rollup-plugin@prerenderer/webpack-plugin@purista/core@fallin7/react-svg-timelinezustand-middlewares@qte/nest-config@sabinmarcu/eslint-config@salsita/salsa-ui@ntegra/ui@rafterjs/di-autoloader@scalingparrots/tsoa-clivite-plugin-i18next-loader@overlapmedia/imagemapper@oxheadalpha/tezos-pulumi@ouc/docusaurus-plugin@micra/micra-graphql-container@mollbe/theme-compiler@minimalui/core@lukasbach/scaffold@markmctamney/eslint-config@maninak/eslint-config@inventivetalent/gitconfigsnowball-typings@rotorsoft/eventually-openapi@nxg-org/mineflayer-mitm-proxy@razorpay/blade@refastdev/create-refast-app@refastdev/refast-devdesign-system-v-testvue3-ajv-formxiorcreate-s4-botyaml-config-tsyb-eks-blueprints@kendallroth/starter-kit-api@kapustlo/httpzero-configuration@edgarsilva948/eks-blueprints@flopflip/react@flopflip/launchdarkly-adapter@flopflip/splitio-adapter@gitkitjs/gitkitjs
7.0.0

3 months ago

6.2.1

3 months ago

6.1.0

11 months ago

6.2.0

10 months ago

6.0.1

1 year ago

6.0.0

1 year ago

6.0.3

1 year ago

6.0.2

1 year ago

5.0.0

1 year ago

4.0.0

2 years ago

3.0.0

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.0

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago