|
2 | 2 |
|
3 | 3 | 数组合并了相同类型的对象,而元组(Tuple)合并了不同类型的对象。 |
4 | 4 |
|
5 | | -元组起源于函数编程语言(如 F#),在这些语言中频繁使用元组。 |
| 5 | +元组起源于函数编程语言(如 F#),这些语言中会频繁使用元组。 |
6 | 6 |
|
7 | 7 | ## 简单的例子 |
8 | 8 |
|
9 | 9 | 定义一对值分别为 `string` 和 `number` 的元组: |
10 | 10 |
|
11 | 11 | ```ts |
12 | | -let xcatliu: [string, number] = ['Xcat Liu', 25]; |
| 12 | +let tom: [string, number] = ['Tom', 25]; |
13 | 13 | ``` |
14 | 14 |
|
15 | 15 | 当赋值或访问一个已知索引的元素时,会得到正确的类型: |
16 | 16 |
|
17 | 17 | ```ts |
18 | | -let xcatliu: [string, number]; |
19 | | -xcatliu[0] = 'Xcat Liu'; |
20 | | -xcatliu[1] = 25; |
| 18 | +let tom: [string, number]; |
| 19 | +tom[0] = 'Tom'; |
| 20 | +tom[1] = 25; |
21 | 21 |
|
22 | | -xcatliu[0].slice(1); |
23 | | -xcatliu[1].toFixed(2); |
| 22 | +tom[0].slice(1); |
| 23 | +tom[1].toFixed(2); |
24 | 24 | ``` |
25 | 25 |
|
26 | 26 | 也可以只赋值其中一项: |
27 | 27 |
|
28 | 28 | ```ts |
29 | | -let xcatliu: [string, number]; |
30 | | -xcatliu[0] = 'Xcat Liu'; |
| 29 | +let tom: [string, number]; |
| 30 | +tom[0] = 'Tom'; |
31 | 31 | ``` |
32 | 32 |
|
33 | 33 | 但是当直接对元组类型的变量进行初始化或者赋值的时候,需要提供所有元组类型中指定的项。 |
34 | 34 |
|
35 | 35 | ```ts |
36 | | -let xcatliu: [string, number]; |
37 | | -xcatliu = ['Xcat Liu', 25]; |
| 36 | +let tom: [string, number]; |
| 37 | +tom = ['Tom', 25]; |
38 | 38 | ``` |
39 | 39 |
|
40 | 40 | ```ts |
41 | | -let xcatliu: [string, number] = ['Xcat Liu']; |
| 41 | +let tom: [string, number]; |
| 42 | +tom = ['Tom']; |
42 | 43 |
|
43 | | -// index.ts(1,5): error TS2322: Type '[string]' is not assignable to type '[string, number]'. |
44 | | -// Property '1' is missing in type '[string]'. |
45 | | -``` |
46 | | - |
47 | | -```ts |
48 | | -let xcatliu: [string, number]; |
49 | | -xcatliu = ['Xcat Liu']; |
50 | | -xcatliu[1] = 25; |
51 | | - |
52 | | -// index.ts(2,1): error TS2322: Type '[string]' is not assignable to type '[string, number]'. |
53 | | -// Property '1' is missing in type '[string]'. |
| 44 | +// Property '1' is missing in type '[string]' but required in type '[string, number]'. |
54 | 45 | ``` |
55 | 46 |
|
56 | 47 | ## 越界的元素 |
57 | 48 |
|
58 | 49 | 当添加越界的元素时,它的类型会被限制为元组中每个类型的联合类型: |
59 | 50 |
|
60 | 51 | ```ts |
61 | | -let xcatliu: [string, number]; |
62 | | -xcatliu = ['Xcat Liu', 25]; |
63 | | -xcatliu.push('http://xcatliu.com/'); |
64 | | -xcatliu.push(true); |
| 52 | +let tom: [string, number]; |
| 53 | +tom = ['Tom', 25]; |
| 54 | +tom.push('male'); |
| 55 | +tom.push(true); |
65 | 56 |
|
66 | | -// index.ts(4,14): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. |
67 | | -// Type 'boolean' is not assignable to type 'number'. |
| 57 | +// Argument of type 'true' is not assignable to parameter of type 'string | number'. |
68 | 58 | ``` |
69 | 59 |
|
70 | | - |
71 | 60 | ## 参考 |
72 | 61 |
|
73 | 62 | - [Basic Types # Tuple](http://www.typescriptlang.org/docs/handbook/basic-types.html#tuple)([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html#元组-tuple)) |
|
0 commit comments