Aller au contenu principal

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 };