Skip to content

Instantly share code, notes, and snippets.

@vikiboss
Created April 20, 2023 08:10
Show Gist options
  • Save vikiboss/c2babef2f6ae43a2aac2886d774ad476 to your computer and use it in GitHub Desktop.
Save vikiboss/c2babef2f6ae43a2aac2886d774ad476 to your computer and use it in GitHub Desktop.
destructuring-with-object-or-array
// @from antfu https://antfu.me/posts/destructuring-with-object-or-array
export function destructIt<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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment