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's type definition should support keys that don't exist in the object being picked from #838

Open
ben519 opened this issue Nov 22, 2024 · 1 comment

Comments

@ben519
Copy link

ben519 commented Nov 22, 2024

Consider this example:

interface Person {
  id: string;
  age: number;
}

const person: Person = { id: "123", age: 30 };
const picked = pick(person, ["id", "name"] as const);

This generates the error

Type '"name"' is not assignable to type 'keyof Person'.

Screenshot 2024-11-22 at 10 09 29 AM

My understanding is that this should be possible; pick() should simply ignore/exclude keys that don't exist in the object. At least, that's the way I've always used it with vanilla JavaScript.

@raon0211
Copy link
Collaborator

Hi there! 😊

I understand that the behavior differs from vanilla JavaScript. However, this is intentional in our library. In es-toolkit, our design principle is to provide strict types wherever possible.

If you want to pick properties as shown in the example code, you can cast the property key array to either any[] or Array<keyof Person>.

Let me know if you need further clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants