Skip to content

Documentation / @andrew_l/toolkit / deepAssign

Function: deepAssign()

deepAssign(dest, source): void

Performs a deep merge of the source object into the destination object.

This function recursively copies properties from the source object to the destination object. If a property is an object itself, it will recursively merge its properties. Otherwise, the value will be directly assigned to the destination object.

⚠️ Mutates the destination object: The destination object is modified in place.

Parameters

dest

object

The target object that will be modified with properties from the source.

source

object

The source object whose properties will be copied to the destination.

Returns

void

This function does not return a value, as it mutates the destination object.

Examples

ts
const user = {
  id: 1,
  name: 'Andrew',
  data: { a: 1, b: 2 },
};

deepAssign(user, { data: { c: 3 } });

console.log(user);
// Outputs: '{ id: 1, name: 'Andrew', data: { a: 1, b: 2, c: 3 } }'
ts
const config = { theme: { dark: true }, version: '1.0' };
const updates = { theme: { light: false }, version: '2.0' };

deepAssign(config, updates);

console.log(config);
// Outputs: '{ theme: { dark: true, light: false }, version: '2.0' }'