【发布时间】:2023-03-14 08:40:01
【问题描述】:
我有这个功能可以合并任意数量的对象
function merge(...objs) {
return objs.reduce((res, cur) => {
for (const key in cur) {
res[key] = cur[key]
}
return res;
}, {});
}
一开始我以为这个函数不能进行类型注解,但后来我尝试了与我的merge函数非常相似的rest参数
const obj = {
...{ name: { ownName: 'Lewis' } },
...{ link: 'google.com' }
}
type Obj = typeof obj // I can happily get the Obj type
然后我想到了一个想法:当你事先不知道类型时,使用泛型。但是我如何定义其他泛型类型,例如
function merge<T, U, V...>(...objs: Array<T | U | V...>)
【问题讨论】:
标签: javascript typescript merge typescript-generics