Skip to content

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

Open
@yuhr

Description

@yuhr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    collectionsfeedback welcomeWe want community's feedback on this issue or PRsuggestiona suggestion yet to be agreed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions