Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pick and omit should preserve prototype, property descriptors, and extensibility #5927

Open
yuhr opened this issue Sep 9, 2024 · 1 comment
Labels
collections feedback welcome We want community's feedback on this issue or PR suggestion a suggestion yet to be agreed

Comments

@yuhr
Copy link
Contributor

yuhr commented Sep 9, 2024

Is your feature request related to a problem? Please describe.

I'm not sure this is classified as a bug or a feature request, but maybe rather a proposal thing.

As I wrote in #5922 (comment), I think filtering functions like pick and omit should preserve the semantics of the original object as much as possible. Thus the prototype, the property descriptors, and the extensibility should be copied to the result.

Describe the solution you'd like

For example, the code of pick should be like below:

export function pick<T extends object, K extends keyof T>(
  obj: Readonly<T>,
  keys: readonly K[],
): Pick<T, K> {
  const result = Object.create(Object.getPrototypeOf(obj));
  for (const key of keys) {
    const descriptor = Object.getOwnPropertyDescriptor(obj, key);
    if (descriptor) Object.defineProperty(result, key, descriptor);
  }
  if (!Object.isExtensible(obj)) Object.preventExtensions(result);
  return result;
}

Is this an acceptable change to the APIs? If so, could someone enumerate APIs that should be rewritten?

  • pick
  • omit
@yuhr yuhr changed the title pick and omit should preserve prototype and property descriptors pick and omit should preserve prototype, property descriptors, and extensibility Sep 9, 2024
@iuioiua
Copy link
Contributor

iuioiua commented Sep 9, 2024

Idk, this seems like over-engineering an otherwise simple function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collections feedback welcome We want community's feedback on this issue or PR suggestion a suggestion yet to be agreed
Projects
None yet
Development

No branches or pull requests

2 participants