| [a762898] | 1 | /**
|
|---|
| 2 | * Merges the properties of the source object into a deep clone of the target object.
|
|---|
| 3 | * Unlike `merge`, This function does not modify the original target object.
|
|---|
| 4 | *
|
|---|
| 5 | * This function performs a deep merge, meaning nested objects and arrays are merged recursively.
|
|---|
| 6 | *
|
|---|
| 7 | * - 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.
|
|---|
| 8 | * - If a property in the source object is undefined, it will not overwrite a defined property in the target object.
|
|---|
| 9 | *
|
|---|
| 10 | * Note that this function does not mutate the target object.
|
|---|
| 11 | *
|
|---|
| 12 | * @param {T} target - The target object to be cloned and merged into. This object is not modified directly.
|
|---|
| 13 | * @param {S} source - The source object whose properties will be merged into the cloned target object.
|
|---|
| 14 | * @returns {T & S} A new object with properties from the source object merged into a deep clone of the target object.
|
|---|
| 15 | *
|
|---|
| 16 | * @template T - Type of the target object.
|
|---|
| 17 | * @template S - Type of the source object.
|
|---|
| 18 | *
|
|---|
| 19 | * @example
|
|---|
| 20 | * const target = { a: 1, b: { x: 1, y: 2 } };
|
|---|
| 21 | * const source = { b: { y: 3, z: 4 }, c: 5 };
|
|---|
| 22 | *
|
|---|
| 23 | * const result = toMerged(target, source);
|
|---|
| 24 | * console.log(result);
|
|---|
| 25 | * // Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }
|
|---|
| 26 | *
|
|---|
| 27 | * @example
|
|---|
| 28 | * const target = { a: [1, 2], b: { x: 1 } };
|
|---|
| 29 | * const source = { a: [3], b: { y: 2 } };
|
|---|
| 30 | *
|
|---|
| 31 | * const result = toMerged(target, source);
|
|---|
| 32 | * console.log(result);
|
|---|
| 33 | * // Output: { a: [3, 2], b: { x: 1, y: 2 } }
|
|---|
| 34 | *
|
|---|
| 35 | * @example
|
|---|
| 36 | * const target = { a: null };
|
|---|
| 37 | * const source = { a: [1, 2, 3] };
|
|---|
| 38 | *
|
|---|
| 39 | * const result = toMerged(target, source);
|
|---|
| 40 | * console.log(result);
|
|---|
| 41 | * // Output: { a: [1, 2, 3] }
|
|---|
| 42 | */
|
|---|
| 43 | declare function toMerged<T extends Record<PropertyKey, any>, S extends Record<PropertyKey, any>>(target: T, source: S): T & S;
|
|---|
| 44 |
|
|---|
| 45 | export { toMerged };
|
|---|