数组的类型注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const numberArr: number[] = [1, 2, 3]
const stringArr: string[] = ['a', 'b']
const undefinedArr: undefined[] = [undefined, undefined]
const arr: (number | string | boolean)[] = [1, 'a', true, false] // 用到联合类型

// 类型别名 type alias
type lady = { name: string, age: number }

const xiaojj: lady[] = [
{
name: 'xiaojj',
age: 90
},
{
name: 'j',
age: 30
}
]

// 或用类的方式
class Ady2 {
name: string;
age: number;
}

const xiaojj2: Ady2[] = [
{
name: 'xiaojj',
age: 90
},
{
name: 'j',
age: 30
}
]