deepMerge

A TypeScript utility function to perform a deep merge of two objects, allowing for selective merging of nested properties.
> deepMerge<T extends object>({ target, source }: DeepMergeProps<T>): T
Parameters
  • target (object): The original object to merge into. This is the base object in the merge operation.
  • source (object): The object containing updates. Only properties in `source` will overwrite target properties.
  • Returns: An object of type `T` with `target` properties overwritten by `source` properties where applicable.

Example

import { deepMerge } from "@explita/daily-toolset";

const original = {
    user: {
    name: 'John',
    address: {
        city: 'New York',
        zip: '10001'
    }
    }
};

const updates = {
    user: {
    address: {
        city: 'San Francisco'
    }
    }
};

const merged = deepMerge({ target: original, source: updates });
console.log(merged);
/*
{
    user: {
    name: 'John',
    address: {
        city: 'San Francisco',
        zip: '10001'
    }
    }
}
*/