问题
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