ts 联合类型

ts 联合类型

参考:https://www.jianshu.com/p/655a6471f50f

指多个类型的合并类型

1. 基础类型联合

let a: string | number;
a = 1; //ok 
a= "a"//ok

2. 对象类型联合

对象联合类型只能访问联合中所有共同成员

interface Women{
  age: number,
  sex: string,
  cry(): void
}
interface Man{
  age: number,
  sex: string,
}
declare function People(): Women | Man;
let people = People();
people.age = 18; //ok
people.cry();//error 非共同成员
全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务