The type of the target object
The target object to merge into
The source object to merge from. Null or undefined source is safely ignored.
A function (key, srcValue, tgtValue) => boolean called for each own
enumerable safe property key of source (string and symbol). The property is merged only
when the predicate returns truthy.
The target object (mutated in place).
const target = { a: 1, b: 2 };
const source = { b: 99, c: 3 };
// Only merge keys whose source value is greater than 2
objMergeIf(target, source, (key, srcVal) => srcVal > 2);
// target => { a: 1, b: 99, c: 3 }
// Only merge when the key does not already exist in target
const t2 = { x: 10 };
objMergeIf(t2, { x: 99, y: 5 }, (key, _sv, tgtVal) => tgtVal === undefined);
// t2 => { x: 10, y: 5 }
// Unsafe keys are filtered even if predicate returns true
const t3: any = {};
objMergeIf(t3, { constructor: "ignored", safe: 1 } as any, () => true);
// t3 => { safe: 1 }
Copies own enumerable properties from
sourcetotargetonly when the predicate returns truthy for that key/value pair. All other own properties ontargetare left unchanged.Security behavior: this helper iterates source keys via forEachOwnKeySafe, so unsafe keys (
__proto__,constructor,prototype) are ignored even if the predicate returns true. It also skips all writes whentargetis a guarded built-in prototype object per isUnsafeTarget.