1. 联合类型 |
指多个类型的合并类型
1.基础类型联合
let a: string | number;
a = 1; //ok
a= "a"//ok
- 对象类型联合
对象联合类型只能访问联合中所有共同成员
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 非共同成员
2. 交叉类型 &
多种类型的集合,联合对象将具有所联合类型的所有成员
interface People {
age: number,
height: number
}
interface Man{
sex: string
}
const lilei = (man: People & Man) => {
console.log(man.age)
console.log(man.height)
console.log(man.sex)
}
lilei({age: 18,height: 180,sex: 'male'});