Destructuring

coderljw 2024-10-13 Utils
  • Utils
  • Destructuring
小于 1 分钟

# 1. 对象数组通用结构

const destructuring = <T extends Record<string, unknown>, A extends readonly any[]>(
  obj: T,
  arr: A,
): T & A => {
  const clone = { ...obj }

  Object.defineProperty(clone, Symbol.iterator, {
    enumerable: false,
    value() {
      let index = 0
      return {
        next: () => ({
          value: arr[index++],
          done: index > arr.length,
        }),
      }
    },
  })

  return clone as T & A
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
以父之名
周杰伦.mp3