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
-
targettype target = unknown;- Target value to merge into.
-
sourcetype source = unknown;- Source value to merge from.
-
shouldMutateTargettype 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`.