TS 进阶笔记

101 阅读1分钟

TS类型系统

概览

交叉类型

function extend<T extends object,U extends object>(first:T,second:U):T&U{
  const result = <T&U>{}
  for(let id in first){
    (<T>result)[id] = first[id]
  }
  for(let id in second){
    if(!result.hasOwnProperty(id)){
      (<U>result)[id] = second[id]
    }
  }
  return result
}

const x = extend({a:'hello'},{b:456})

console.log(x.a);
console.log(x.b);