Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa8cc18

Browse files
zenghongtuhongtu-lokation
authored andcommitted
fixed tuple
1 parent 8be7878 commit aa8cc18

File tree

1 file changed

+1
-20
lines changed

1 file changed

+1
-20
lines changed

advanced/tuple.md

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff 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
6861
let 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

0 commit comments

Comments
 (0)