File tree Expand file tree Collapse file tree 2 files changed +2
-22
lines changed Expand file tree Collapse file tree 2 files changed +2
-22
lines changed Original file line number Diff line number Diff line change @@ -55,14 +55,7 @@ xcatliu[1] = 25;
5555
5656## 越界的元素
5757
58- 当赋值给越界的元素时,它类型会被限制为元组中每个类型的联合类型:
59-
60- ``` ts
61- let xcatliu: [string , number ];
62- xcatliu = [' Xcat Liu' , 25 , ' http://xcatliu.com/' ];
63- ```
64-
65- 上面的例子中,数组的第三项满足联合类型 ` string | number ` 。
58+ 当添加越界的元素时,它的类型会被限制为元组中每个类型的联合类型:
6659
6760``` ts
6861let xcatliu: [string , number ];
@@ -74,18 +67,6 @@ xcatliu.push(true);
7467// Type 'boolean' is not assignable to type 'number'.
7568```
7669
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#访问联合类型的属性或方法 )
8970
9071## 参考
9172
Original file line number Diff line number Diff line change @@ -11,8 +11,7 @@ type NameOrResolver = Name | NameResolver;
1111function getName(n : NameOrResolver ): Name {
1212 if (typeof n === ' string' ) {
1313 return n ;
14- }
15- else {
14+ } else {
1615 return n ();
1716 }
1817}
You can’t perform that action at this time.
0 commit comments