extend()
extend<
Target,Source>(object, ...sources):Target&Source
Assigns own enumerable string keyed properties of source objects to the destination object.
Alias for assign.
DEPRECATED
Use Object.assign() or spread syntax directly instead.
Reason:
Alias of assign
Type Parametersโ
Target: Target extends Record<string, unknown>โ
The type of the destination object.
Source: Source extends Record<string, unknown>โ
The type of the source objects.
Parametersโ
object: Targetโ
The destination object.
sources: ...Source[]โ
The source objects.
Returns: Target & Sourceโ
The destination object.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
extend (Lodash) ยท merge (Remeda, Modern Dash) ยท mergeRight (Ramda) ยท โ (es-toolkit, Radashi, Effect, Antfu)
Exampleโ
const target = { a: 1, b: 2 };
const source1 = { b: 3, c: 4 };
const source2 = { c: 5, d: 6 };
// โ Deprecated approach
const result = extend(target, source1, source2);
console.log(result); // { a: 1, b: 3, c: 5, d: 6 }
// โ
Recommended approach (mutable)
const resultNative = Object.assign(target, source1, source2);
console.log(resultNative); // { a: 1, b: 3, c: 5, d: 6 }
// โ
Recommended approach (immutable)
const resultImmutable = { ...target, ...source1, ...source2 };
console.log(resultImmutable); // { a: 1, b: 3, c: 5, d: 6 }
How it works?โ
Assigns source properties to destination (alias for assign).
Deprecated: Use Object.assign() or spread operator.
Native Equivalentโ
// โ extend(target, source)
// โ
Object.assign(target, source)
// โ
{ ...target, ...source }
Use Casesโ
Merge objects ๐โ
Merge multiple objects into target.
const result = { ...base, ...overrides };
// Or mutating:
Object.assign(target, source1, source2);
Apply defaultsโ
Merge user options with defaults.
const options = { ...defaults, ...userOptions };