Skip to main content

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