🌙 普通类型
interface IName {
firstName: string;
lastName: string;
}
interface IBaseInfo {
sex: 'male' | 'female';
age: number;
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
🌙 联合类型(| - 或)
type IPerson = IName | IBaseInfo;
1
🌙 交叉类型(& - 与)
交叉类型是将多个类型合并为一个类型
type IProps = IProps1 & IProps2;
1
const user: IUserInfo = {
firstName: '1',
lastName: '2',
sex: 'male',
age: 11,
}
1
2
3
4
5
6
2
3
4
5
6
🌙 never
(空集)
interface IProps1 {
size: string;
}
interface IProps2 {
size: number;
}
// never
type IProps = IProps1 & IProps2;
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
never的作用:
type AllType = 'a' | 'b' | 'c';
function handleValue(val: AllType) {
switch (val) {
case 'a':
// val 在这里收窄为 'a'
break;
case 'b':
// val 在这里收窄为 'b'
break;
default:
// val 在这里收窄为 never
const exhaustiveCheck: never = val;
break;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16