我有一个函数,它接收字符串数组并返回一个对象,其键是字符串,每个值是undefined:
function getInitialCharacteristics(names: string[]): ??? {
return names.reduce((obj, name) => ({ ...obj, [name]: undefined }), {});
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
const result = getInitialCharacteristics(["hello", "world"]);
// result == { hello: undefined, world: undefined }
Run Code Online (Sandbox Code Playgroud)
现在我想知道如何定义使用 TypeScript 的正确返回值getInitialCharacteristics。我必须使用泛型或以某种方式动态生成该类型。那可能吗?