Skip to content

mergeTwoObjects

type: function

The mergeTwoObjects function recursively merges source properties into target when both sides are objects.

If either input is not an object, it returns source.

Usage

import { mergeTwoObjects } from '@mustib/utils';
const target = { a: 1, b: { c: 2 } };
const source = { b: { d: 3 }, e: 4 };
const result = mergeTwoObjects(target, source);
// { a: 1, b: { c: 2, d: 3 }, e: 4 }

Definition

function mergeTwoObjects<Target, Source>(
target: Target,
source: Source,
shouldMutateTarget = false,
): MergeObjects<Target, Source> { }

Parameters

  1. target
    type target = unknown;
    • Target value to merge into.
  2. source
    type source = unknown;
    • Source value to merge from.
  3. shouldMutateTarget
    type shouldMutateTarget = boolean;
    /* default value */
    const shouldMutateTarget = false;
    • When false, merges into a structured clone of target. When true, mutates target directly.

Returns

type T = MergeObjects<Target, Source>;
  • Merged object when both inputs are objects.
  • Otherwise returns `source`.