类型推论 、类型别名
# 类型推论和类型别名
# 类型推论
let a = '123'
// 默认会推出字符串
let b
b = 456
b = null
// 默认为any,天然的类型推论
1
2
3
4
5
6
7
2
3
4
5
6
7
# 类型别名
type s = (name: string) => void
type s = A & B // 只能使用交叉类型
interface A extends B {
}
interface B {
}
// 1.type 是无法继承的
type s = number | string
type s = A & B // 2.联合类型
interface B {
age: number | string
// 只能写在里面
}
// 3.type 重名不会合并,interface会合并
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# type 高级用法
type num = 1 extends number ? 1:2
// type中extends包含的意思
// 左边的值 会作为右边类型的子类型
// 1.any unknow
// 2.Object
// 3.Number
// 4.number
// 5. never
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
上次更新: 2023/10/22, 16:10:00