const pick = <TObj, TKeys extends (keyof TObj)[]>(obj: TObj, picked: TKeys) => {
return picked.reduce((acc, key) => {
acc[key] = obj[key];
return acc;
}, {} as Pick<TObj, TKeys[number]>);
};
Notice that TKeys
is ("a" | "b")[]
, which is not really good, better to be "a" | "b"
.
Updated:
const pick = <TObj, TKeys extends keyof TObj>(obj: TObj, picked: TKeys[]) => {
return picked.reduce((acc, key) => {
acc[key] = obj[key];
return acc;
}, {} as Pick<TObj, TKeys>);
};
标签:acc,Typescript,return,key,Clean,picked,obj,type
From: https://www.cnblogs.com/Answer1215/p/17170159.html