Skip to content

Function: copyProperties()

ts
function copyProperties<T, U>(target, source): void;

Copies all properties from source to target, including non-enumerable ones. 将所有属性从源对象复制到目标对象,包括不可枚举的属性。

Type Parameters

Type Parameter
T
U extends Record<string, unknown>

Parameters

ParameterTypeDescription
targetTThe target object to copy properties to / 要复制属性到的目标对象
sourceUThe source object to copy properties from / 要从中复制属性的源对象

Returns

void

Example

typescript
const source = { a: 1, b: 2 }
const target = { c: 3 }
copyProperties(target, source)
target // => { c: 3, a: 1, b: 2 }

// Copies non-enumerable properties too
const src = {}
Object.defineProperty(src, 'hidden', { value: 'secret', enumerable: false, writable: true, configurable: true })
const tgt = {}
copyProperties(tgt, src)
Object.getOwnPropertyDescriptor(tgt, 'hidden')
// => { value: 'secret', writable: true, enumerable: false, configurable: true }

Since

1.0.0

Released under the MIT License.