Skip to content

assign

将多个对象的属性合并到一个新对象中。

语法

typescript
assign<T extends Record<string, any>>(
  target: T,
  ...sources: Partial<T>[]
): T

参数

  • target (T): 目标对象
  • ...sources (Partial<T>[]): 要合并的源对象

返回值

  • T: 合并后的新对象

示例

基本用法

typescript
import { assign } from 'radash'

const user = { name: 'Alice', age: 25 }
const updates = { age: 26, city: 'New York' }

const updatedUser = assign(user, updates)
// { name: 'Alice', age: 26, city: 'New York' }

合并多个对象

typescript
import { assign } from 'radash'

const base = { name: 'Alice', age: 25 }
const updates1 = { age: 26 }
const updates2 = { city: 'New York' }
const updates3 = { country: 'USA' }

const result = assign(base, updates1, updates2, updates3)
// { name: 'Alice', age: 26, city: 'New York', country: 'USA' }

处理嵌套对象

typescript
import { assign } from 'radash'

const user = {
  name: 'Alice',
  profile: {
    age: 25,
    city: 'Boston'
  }
}

const updates = {
  profile: {
    age: 26,
    country: 'USA'
  }
}

const updatedUser = assign(user, updates)
// {
//   name: 'Alice',
//   profile: {
//     age: 26,
//     country: 'USA'
//   }
// }

处理数组

typescript
import { assign } from 'radash'

const config = {
  settings: {
    theme: 'dark',
    features: ['feature1', 'feature2']
  }
}

const updates = {
  settings: {
    features: ['feature3', 'feature4']
  }
}

const updatedConfig = assign(config, updates)
// {
//   settings: {
//     theme: 'dark',
//     features: ['feature3', 'feature4']
//   }
// }

处理函数属性

typescript
import { assign } from 'radash'

const component = {
  name: 'Button',
  render: () => '<button>Click me</button>',
  props: { text: 'Click me' }
}

const updates = {
  render: () => '<button>Updated</button>',
  props: { text: 'Updated' }
}

const updatedComponent = assign(component, updates)
// {
//   name: 'Button',
//   render: () => '<button>Updated</button>',
//   props: { text: 'Updated' }
// }

处理日期对象

typescript
import { assign } from 'radash'

const event = {
  title: 'Meeting',
  date: new Date('2023-01-01'),
  attendees: ['Alice', 'Bob']
}

const updates = {
  date: new Date('2023-01-02'),
  attendees: ['Alice', 'Bob', 'Charlie']
}

const updatedEvent = assign(event, updates)
// {
//   title: 'Meeting',
//   date: new Date('2023-01-02'),
//   attendees: ['Alice', 'Bob', 'Charlie']
// }

处理空值

typescript
import { assign } from 'radash'

const user = {
  name: 'Alice',
  email: 'alice@example.com',
  phone: '123-456-7890'
}

const updates = {
  email: null,
  phone: undefined,
  city: 'New York'
}

const updatedUser = assign(user, updates)
// {
//   name: 'Alice',
//   email: null,
//   phone: undefined,
//   city: 'New York'
// }

处理类型安全

typescript
import { assign } from 'radash'

interface User {
  name: string
  age: number
  email?: string
}

const user: User = {
  name: 'Alice',
  age: 25
}

const updates: Partial<User> = {
  age: 26,
  email: 'alice@example.com'
}

const updatedUser = assign(user, updates)
// TypeScript 会正确推断类型

处理只读属性

typescript
import { assign } from 'radash'

const config = {
  readonly: 'cannot change',
  mutable: 'can change'
} as const

const updates = {
  mutable: 'updated value'
}

const updatedConfig = assign(config, updates)
// {
//   readonly: 'cannot change',
//   mutable: 'updated value'
// }

处理Symbol属性

typescript
import { assign } from 'radash'

const sym = Symbol('key')

const obj1 = {
  [sym]: 'value1',
  regular: 'prop1'
}

const obj2 = {
  [sym]: 'value2',
  regular: 'prop2'
}

const result = assign(obj1, obj2)
// {
//   [sym]: 'value2',
//   regular: 'prop2'
// }

处理不可枚举属性

typescript
import { assign } from 'radash'

const obj1 = {}
Object.defineProperty(obj1, 'hidden', {
  value: 'secret',
  enumerable: false
})

const obj2 = { visible: 'public' }

const result = assign(obj1, obj2)
// { visible: 'public' }
// hidden 属性不会被复制

注意事项

  1. 浅拷贝: 只进行浅拷贝,嵌套对象会被引用
  2. 不可枚举属性: 不会复制不可枚举的属性
  3. Symbol属性: 会复制Symbol属性
  4. 原型链: 不会复制原型链上的属性

与其他函数的区别

  • assign: 合并对象属性
  • Object.assign: 原生方法,功能相同
  • spread: 使用展开运算符
  • merge: 深度合并对象

性能

  • 时间复杂度: O(n × m),其中 n 是源对象数量,m 是属性数量
  • 空间复杂度: O(k),其中 k 是目标对象的属性数量
  • 适用场景: 对象属性合并和更新

Released under the MIT License.