toss / es-toolkit

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.
https://es-toolkit.slash.page
Other
7.07k stars 325 forks source link

pick's type definition should support keys that don't exist in the object being picked from #838

Open ben519 opened 9 hours ago

ben519 commented 9 hours ago

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.