2
如何爲一個函數編寫返回類型註釋,該函數接受一個對象,調用其所有方法並返回一個新對象,其中原始鍵映射到方法的返回值?用於調用對象方法的返回類型註釋
function callMethods<T>(obj: T) {
const objResults = {};
Object.keys(obj).forEach((prop) => objResults[prop] = obj[prop]({}));
return objResults;
}
type MethodArgs = any // some complex object
console.log(callMethods({
a: (_args: MethodArgs): number => 1,
b: (_args: MethodArgs): string => "one",
c: (_args: MethodArgs): number[] => [1]
}));
// => {a: 1, b: "one", c: [1]}
// This object's type should be {a: number, b: string, c: number[]}