1

So I'm trying to create an utils function that works like _.pick() function from Lodash, but using TypeScript. Basically it is an utils function that would return an object with selected properties, where the first param is the object target and the second param is the array of object properties string that can be chosen based on the object in the first param.

I have found and applied the function (source) and it's working fine, but I'm kind of confused on how to apply the types so it works just like using the original library.

const pick = (object: { [key: string]: any }, keys: string[]) => {
  return keys.reduce<any>((obj, key) => {
    if (object && Object.prototype.hasOwnProperty.call(object, key)) {
      obj[key as keyof typeof obj] = object[key];
    }
    return obj;
  }, {});
};

export default pick;

Use case:

const object = { 'a': 1, 'b': '2', 'c': 3 };
 
const pickedObject = pick(object, ['a', 'c']);
console.log(pickedObject)// => { 'a': 1, 'c': 3 }

This is the function I'm currently using and I'm expecting the second params would have type of array of object properties string based on the object in first param, and the return type would be the object with properties that is chosen from the second param.

Elian
  • 13
  • 4
  • 1
    Does the accepted answer to [this question](https://stackoverflow.com/q/47232518/18057908) solve your problem? – Palladium02 May 19 '23 at 12:46
  • 1
    Does [this approach](https://tsplay.dev/WYQlQW) meet your needs? If so I could write up an answer; if not, what am I missing? (Although there are probably duplicates as the previous comment suggests) – jcalz May 19 '23 at 12:51
  • @jcalz Yes it does! Thankyou! The answer to the question in previous comment seems to be different because the second parameter is not an array of strings. Could you please explain a bit on what is happening on the typescript also? Thankyou so much! – Elian May 19 '23 at 12:56
  • It is very slightly different because it's using a [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) and you can just [change that](https://tsplay.dev/Nn8BBW) to get a version that works for your question. Is there something else that doesn't apply there? – jcalz May 19 '23 at 12:58
  • @jcalz I guess not. Should I just edit this as a duplicate or not? The previous answer seems has a slightly different use case, but your example is the answer I'm looking for. – Elian May 19 '23 at 13:08
  • Have you simply had a look at [the type declared for lodash's `pick`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/16a2befc8fc262d0e75f4ee486e8276181a9fce1/types/lodash/common/object.d.ts#L2050-L2069)? – Bergi May 19 '23 at 14:20
  • 1
    This is marked as a duplicate now. If you want more information about how it works you can look at the documentation for [generics](https://www.typescriptlang.org/docs/handbook/2/generics.html) and the definition of [`Pick`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#partial-readonly-record-and-pick). – jcalz May 19 '23 at 14:29

0 Answers0