How to implement a pluck function in TypeScript?

丶灬走出姿态 提交于 2020-04-16 06:19:09

问题


I often need to pluck properties out of an object

const obj = {a:1, b: 2, c: 3};
const plucked = pluck(obj, 'a', 'b'); // {a: 1, b:2}

However, this is not easy to do in TypeScript if you want type safety because I can't implement a function with the following signature in TypeScript, which I found on TypeScript's manual.

declare function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K>;

I couldn't get a version that compiled, here's a version I tried:

function pluck<T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
    // Type '{}' is not assignable to type 'Pick<T, K>'.
    const ret: Pick<T, K> = {};
    for (let key of keys) { 
        ret[key] = obj[key];
    }
    return ret; 
}

Does this mean I need to use ambient declarations and declare the function in JS?


回答1:


I ended up using any on the object that I manipulate and TypeScript is happy allowing that as a return value. See it in action

function pluck<T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
    const ret: any = {};
    for (let key of keys) { 
        ret[key] = obj[key];
    }
    return ret; 
}

const less = pluck({ a: 1, b: '2', c: true }, ['a', 'c']);



回答2:


Here's an alternative option:

function pluck<T, K extends keyof T>(objs: T, keys: K[]): Pick<T, K> {
  return keys.reduce((result, key) => 
    Object.assign(result, {[key as string]: objs[key]}), {}) as Pick<T, K>;
}


来源:https://stackoverflow.com/questions/49341226/how-to-implement-a-pluck-function-in-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!