tsmerge

Shallow object merge.

MIT 3 个版本
安装
npm install tsmerge
yarn add tsmerge
pnpm add tsmerge
bun add tsmerge
README

tsmerge

A shallow merge function that works nicely with typescript.

In the spirit of making things immutable, it is important to be able to merge objects together in an easy fashion.

In the ideal world, we could have a spread ... operator in typescript like the one supported by JSX:

const options = {...defaults, ...customizations};

which would copy all the fields in defaults and override with any custom fields from customizations.

The new ES6 Object.assign, does what we want, but has a strange syntax.

const options = Object.assign({}, defaults, customizations);

tsmerge is just a friendlier way of doing Object.assign.

Example:

import {merge} from 'tsmerge';
...

const options = merge(defaults, customizations);

Install:

npm install tsmerge --save

Usage:

import {merge} from 'tsmerge';

const defaults = { timeout: 30000, retries: 3 };
const customizations = { retries: 10 };
const options = merge(defaults, customizations);
console.log(options);  // output: { timeout: 30000, retries: 10 }
版本列表
1.0.4 2016-05-01
1.0.1 2016-04-30
1.0.0 2016-04-30