function extend<T, U>(first: T, second: U): T & U {
let result = <T & U>{};
for (let id in first) {
result[id] = (<any>first)[id];
}
for (let id in second) {
if (!result.hasOwnProperty(id)) {
(<any>result)[id] = (<any>second)[id];
}
}
return result;
}
为什么上面的代码不报错,重点是下面这一句:
result[id] = (<any>first)[id];
result不一定具有id属性
id的类型是
keyof T
, 类型T & U
中有这个属性.