Skip to content

deepMerge

Recursively merges two objects into a new object. Nested plain objects are merged deeply, while arrays and other types are overwritten by the source value.

import { deepMerge } from "1o1-utils";
import { deepMerge } from "1o1-utils/deep-merge";
function deepMerge({ target, source }: DeepMergeParams): Record<string, unknown>
NameTypeRequiredDescription
targetRecord<string, unknown>YesThe base object
sourceRecord<string, unknown>YesThe object to merge into target

Record<string, unknown> — A new merged object (neither input is mutated).

deepMerge({
target: { a: 1, b: { x: 10 } },
source: { b: { y: 20 }, c: 3 },
});
// => { a: 1, b: { x: 10, y: 20 }, c: 3 }
  • Throws if target is not a plain object.
  • Throws if source is not a plain object.
  • Only merges plain objects recursively; arrays and other types are overwritten.
  • Uses a stack-based (non-recursive) algorithm to avoid stack overflow on deeply nested objects.

merge objects, recursive merge, combine objects, extend deep

I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use deepMerge to merge user settings with defaults