Skip to main content

Function: mergeDefault()

mergeDefault<A, B>(base: A, overwrites?: B): DeepRequired<A & B>

Deep merges 2 objects. Properties from the second parameter are applied to the first.

Type Parameters

Type Parameter
A extends object
B extends Partial<A>

Parameters

ParameterTypeDescription
baseABase object
overwrites?BOverwrites to apply

Returns

DeepRequired<A & B>

Remark

overwrites is also mutated!

Remark

If the value of a key in overwrites is undefined then the value of that same key in base is used instead!

Remark

This is essentially { ...base, ...overwrites } but recursively

Examples

const base = { a: 0, b: 1 };
const overwrites = {}; // will be { a: 0, b: 1 } after merge
mergeDefault(base, overwrites) // { a: 0, b: 1 }
const base = { a: 0, b: 1 };
const overwrites = { a: 2, i: 3 };
mergeDefault(base, overwrites) // { a: 2, i: 3, b: 1 };
const base = { a: 0, b: 1 };
const overwrites = { a: null };
mergeDefault(base, overwrites) // { a: null, b: 1 };
const base = { a: 0, b: 1 };
const overwrites = { a: undefined };
mergeDefault(base, overwrites) // { a: 0, b: 1 };
const base = { a: null };
const overwrites = { a: { b: 5 } };
mergeDefault(base, overwrites) // { a: { b: 5 } };

Defined in

mergeDefault.ts:43