【发布时间】:2017-06-12 23:04:06
【问题描述】:
我正在尝试让类型与通用数组 reduce 函数一起工作,该函数基本上合并了两个对象。以下 sn-p 是真实代码的转储版本。为什么fl 的类型是{} 而不是IFoo & IBar?
(我知道这个特殊的例子可以很容易地被一个 Object.assign() 调用替换。)
const flatten = <K, T>(prev: K, x: T): K & T => {
return Object.assign(prev, x);
};
interface IFoo {
foo: true;
}
interface IBar {
bar: true;
}
const fooRes: IFoo = { foo: true };
const barRes: IBar = { bar: true };
const fl = [fooRes, barRes].reduce(flatten, {});
console.log(fl); // here, fl : {}
【问题讨论】:
标签: typescript types reduce