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

Skip to content

Commit 21b163a

Browse files
committed
fix xcatliu#33 update tuple.md
1 parent 541c3aa commit 21b163a

File tree

5 files changed

+31
-34
lines changed

5 files changed

+31
-34
lines changed

advanced/tuple.md

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,61 @@
22

33
数组合并了相同类型的对象,而元组(Tuple)合并了不同类型的对象。
44

5-
元组起源于函数编程语言(如 F#),在这些语言中频繁使用元组
5+
元组起源于函数编程语言(如 F#),这些语言中会频繁使用元组
66

77
## 简单的例子
88

99
定义一对值分别为 `string``number` 的元组:
1010

1111
```ts
12-
let xcatliu: [string, number] = ['Xcat Liu', 25];
12+
let tom: [string, number] = ['Tom', 25];
1313
```
1414

1515
当赋值或访问一个已知索引的元素时,会得到正确的类型:
1616

1717
```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;
2121

22-
xcatliu[0].slice(1);
23-
xcatliu[1].toFixed(2);
22+
tom[0].slice(1);
23+
tom[1].toFixed(2);
2424
```
2525

2626
也可以只赋值其中一项:
2727

2828
```ts
29-
let xcatliu: [string, number];
30-
xcatliu[0] = 'Xcat Liu';
29+
let tom: [string, number];
30+
tom[0] = 'Tom';
3131
```
3232

3333
但是当直接对元组类型的变量进行初始化或者赋值的时候,需要提供所有元组类型中指定的项。
3434

3535
```ts
36-
let xcatliu: [string, number];
37-
xcatliu = ['Xcat Liu', 25];
36+
let tom: [string, number];
37+
tom = ['Tom', 25];
3838
```
3939

4040
```ts
41-
let xcatliu: [string, number] = ['Xcat Liu'];
41+
let tom: [string, number];
42+
tom = ['Tom'];
4243

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]'.
5445
```
5546

5647
## 越界的元素
5748

5849
当添加越界的元素时,它的类型会被限制为元组中每个类型的联合类型:
5950

6051
```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);
6556

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'.
6858
```
6959

70-
7160
## 参考
7261

7362
- [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)

examples/test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var tom;
2+
tom = ['Tom', 25];
3+
tom.push('male');
4+
tom.push(true);

examples/test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let tom: [string, number];
2+
tom = ['Tom', 25];
3+
tom.push('male');
4+
tom.push(true);

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@
5252
"lint-md-cli": "^0.1.1",
5353
"npm-run-all": "^4.1.5",
5454
"prettier": "^1.18.2",
55-
"typescript": "^3.5.1"
55+
"typescript": "^3.6.2"
5656
}
5757
}

0 commit comments

Comments
 (0)