denoland / denoland/std

`pick` and `omit` should preserve prototype, property descriptors, and extensibility

Open
#5,927 1 comment 0 reactions 0 assignees View on GitHub
collections feedback welcome suggestion
Dominant language
TypeScript
Stars
3.6k
Forks
681
PR merge metrics
No merged PRs in 30d

Description

**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 , 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:

```typescript
export function pick(
obj: Readonly,
keys: readonly K[],
): Pick {
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`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.