You can use a function that picks certain properties from a given object:
function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K> {
const copy = {} as Pick<T, K>;
keys.forEach(key => copy[key] = obj[key]);
return copy;
}
Then:
let obj = { "name": "someName", "age": 20 };
let copy = pick(obj, "name") as Test;
console.log(copy); // { name: "someName" }
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…