File tree Expand file tree Collapse file tree 1 file changed +1
-20
lines changed Expand file tree Collapse file tree 1 file changed +1
-20
lines changed Original file line number Diff line number Diff line change @@ -55,14 +55,7 @@ xcatliu[1] = 25;
55
55
56
56
## 越界的元素
57
57
58
- 当赋值给越界的元素时,它类型会被限制为元组中每个类型的联合类型:
59
-
60
- ``` ts
61
- let xcatliu: [string , number ];
62
- xcatliu = [' Xcat Liu' , 25 , ' http://xcatliu.com/' ];
63
- ```
64
-
65
- 上面的例子中,数组的第三项满足联合类型 ` string | number ` 。
58
+ 当添加越界的元素时,它的类型会被限制为元组中每个类型的联合类型:
66
59
67
60
``` ts
68
61
let xcatliu: [string , number ];
@@ -74,18 +67,6 @@ xcatliu.push(true);
74
67
// Type 'boolean' is not assignable to type 'number'.
75
68
```
76
69
77
- 当访问一个越界的元素,也会识别为元组中每个类型的联合类型:
78
-
79
- ``` ts
80
- let xcatliu: [string , number ];
81
- xcatliu = [' Xcat Liu' , 25 , ' http://xcatliu.com/' ];
82
-
83
- console .log (xcatliu [2 ].slice (1 ));
84
-
85
- // index.ts(4,24): error TS2339: Property 'slice' does not exist on type 'string | number'.
86
- ```
87
-
88
- 之前提到过,[ 如果一个值是联合类型,我们只能访问此联合类型的所有类型里共有的属性或方法。] ( ../basics/union-types.md#访问联合类型的属性或方法 )
89
70
90
71
## 参考
91
72
You can’t perform that action at this time.
0 commit comments