assign()
assign<
Target,Source>(object, ...sources):Target&Source
Assigns own enumerable string keyed properties of source objects to the destination object.
DEPRECATED
Use Object.assign() or spread syntax directly instead.
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
assign (Lodash, Radashi) · merge (es-toolkit, Remeda, Modern Dash) · mergeRight (Ramda) · ❌ (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 = assign(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 object.
Deprecated: Use Object.assign() or spread operator directly (ES2015).
Native Equivalent
// ❌ assign(target, source)
// ✅ Object.assign(target, source)
// ✅ { ...target, ...source }
Use Cases
Merge objects 📌
Combine objects into one.
Object.assign({}, defaults, options);
// or
{ ...defaults, ...options };
Shallow copy
Create shallow object copy.
const copy = { ...original };
// or
const copy = Object.assign({}, original);
Apply defaults
Merge defaults with provided options.
const config = { ...defaultConfig, ...userConfig };