Skip to content

Commit 0652024

Browse files
committed
Update tuple
1 parent b686319 commit 0652024

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

advanced/tuple.md

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,51 @@
66

77
定义一对值分别为 `string``number` 的元组:
88

9+
```ts
10+
let xcatliu: [string, number] = ['Xcat Liu', 25];
11+
```
12+
13+
当然也可以先定义类型,后赋值:
14+
915
```ts
1016
let xcatliu: [string, number];
1117
xcatliu = ['Xcat Liu', 25];
1218
```
1319

14-
当访问一个已知索引的元素,会得到正确的类型:
20+
当赋值或访问一个已知索引的元素时,会得到正确的类型:
1521

1622
```ts
1723
let xcatliu: [string, number];
18-
xcatliu = ['Xcat Liu', 25, 'http://xcatliu.com/'];
24+
xcatliu[0] = 'Xcat Liu';
25+
xcatliu[1] = 25;
26+
27+
xcatliu[0].slice(1);
28+
xcatliu[1].toFixed(2);
29+
```
30+
31+
即使只赋值其中一项也可以:
1932

20-
xcatliu[1] = 26;
21-
console.log(xcatliu[0].split(' '));
22-
console.log(xcatliu[1].toFixed(2));
33+
```ts
34+
let xcatliu: [string, number];
35+
xcatliu[0] = 'Xcat Liu';
36+
```
37+
38+
但是当直接对元组类型的变量进行初始化或者赋值的时候,需要提供所有元组类型中指定的项。
39+
40+
```ts
41+
let xcatliu: [string, number] = ['Xcat Liu'];
42+
xcatliu[1] = 25;
43+
44+
// index.ts(1,5): error TS2322: Type '[string]' is not assignable to type '[string, number]'.
45+
// Property '1' is missing in type '[string]'.
46+
```
47+
48+
```ts
49+
let xcatliu: [string, number];
50+
xcatliu = ['Xcat Liu'];
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]'.
2354
```
2455

2556
## 越界的元素
@@ -33,10 +64,11 @@ xcatliu = ['Xcat Liu', 25, 'http://xcatliu.com/'];
3364

3465
```ts
3566
let xcatliu: [string, number];
36-
xcatliu = ['Xcat Liu', 25, 'http://xcatliu.com/'];
67+
xcatliu = ['Xcat Liu', 25];
68+
xcatliu.push('http://xcatliu.com/');
3769
xcatliu.push(true);
3870

39-
// index.ts(3,14): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'.
71+
// index.ts(4,14): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'.
4072
// Type 'boolean' is not assignable to type 'number'.
4173
```
4274

@@ -51,11 +83,13 @@ console.log(xcatliu[2].slice(1));
5183
// index.ts(4,24): error TS2339: Property 'slice' does not exist on type 'string | number'.
5284
```
5385

54-
前面提到过[如果一个值是联合类型,我们只能访问此联合类型的所有类型里共有的成员。](https://xcatliu.gitbooks.io/from-javascript-to-typescript/content/basics/basic-types.html#联合类型(Union+Types))
86+
之前提到过[如果一个值是联合类型,我们只能访问此联合类型的所有类型里共有的成员。](https://xcatliu.gitbooks.io/from-javascript-to-typescript/content/basics/basic-types.html#联合类型(Union+Types))
5587

56-
> Tip: 元组的概念来源于 [C#](https://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx)[Python](http://www.tutorialspoint.com/python/python_tuples.htm)。区别是,C# 和 Python 中的元组都是只读的。
88+
> Tip: 元组的概念来源于 [C#][Python](http://www.tutorialspoint.com/python/python_tuples.htm)。区别是,C# 和 Python 中的元组都是只读的。
5789
5890
## Links
5991

6092
- [Handbook - Basic Types # Tuple](http://www.typescriptlang.org/docs/handbook/basic-types.html#tuple)
6193
- [中文手册 - 基础类型 # 元组 Tuple](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html#元组-tuple)
94+
95+
[C#]: https://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx

examples/playground/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
var xcatliu;
2-
xcatliu = ['Xcat Liu', 25, 'http://xcatliu.com/'];
3-
console.log(xcatliu[2].slice(1));
2+
xcatliu = ['Xcat Liu', 25];
3+
xcatliu.push('http://xcatliu.com/');
4+
xcatliu.push(true);

examples/playground/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
let xcatliu: [string, number];
2-
xcatliu = ['Xcat Liu', 25, 'http://xcatliu.com/'];
3-
4-
console.log(xcatliu[2].slice(1));
2+
xcatliu = ['Xcat Liu', 25];
3+
xcatliu.push('http://xcatliu.com/');
4+
xcatliu.push(true);

0 commit comments

Comments
 (0)