toMerged<T extends Record<PropertyKey, any>,S extends Record<PropertyKey, any>,>(target: T,source: S,): T & S
Merges the properties of the source object into a deep clone of the target object.
Unlike merge
, This function does not modify the original target object.
This function performs a deep merge, meaning nested objects and arrays are merged recursively.
- If a property in the source object is an array or object and the corresponding property in the target object is also an array or object, they will be merged.
- If a property in the source object is undefined, it will not overwrite a defined property in the target object.
Note that this function does not mutate the target object.
const target = { a: 1, b: { x: 1, y: 2 } };
const source = { b: { y: 3, z: 4 }, c: 5 };
const target = { a: 1, b: { x: 1, y: 2 } }; const source = { b: { y: 3, z: 4 }, c: 5 };
const result = toMerged(target, source); console.log(result); // Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }