Skip to content

Commit 7139952

Browse files
committed
fix xcatliu#41 添加了用接口表示数组的用例
1 parent 21b163a commit 7139952

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

basics/type-of-array.md

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,23 @@ let fibonacci: number[] = [1, 1, 2, 3, 5];
1515
```ts
1616
let fibonacci: number[] = [1, '1', 2, 3, 5];
1717

18-
// index.ts(1,5): error TS2322: Type '(number | string)[]' is not assignable to type 'number[]'.
19-
// Type 'number | string' is not assignable to type 'number'.
20-
// Type 'string' is not assignable to type 'number'.
18+
// Type 'string' is not assignable to type 'number'.
2119
```
2220

23-
上例中,`[1, '1', 2, 3, 5]` 的类型被推断为 `(number | string)[]`,这是联合类型和数组的结合。
24-
2521
数组的一些方法的参数也会根据数组在定义时约定的类型进行限制:
2622

2723
```ts
2824
let fibonacci: number[] = [1, 1, 2, 3, 5];
2925
fibonacci.push('8');
3026

31-
// index.ts(2,16): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
27+
// Argument of type '"8"' is not assignable to parameter of type 'number'.
3228
```
3329

34-
上例中,`push` 方法只允许传入 `number` 类型的参数,但是却传了一个 `string` 类型的参数,所以报错了。
30+
上例中,`push` 方法只允许传入 `number` 类型的参数,但是却传了一个 `"8"` 类型的参数,所以报错了。这里 `"8"` 是一个字符串字面量类型,会在后续章节中详细介绍
3531

3632
## 数组泛型
3733

38-
也可以使用数组泛型(Array Generic) `Array<elemType>` 来表示数组:
34+
我们也可以使用数组泛型(Array Generic) `Array<elemType>` 来表示数组:
3935

4036
```ts
4137
let fibonacci: Array<number> = [1, 1, 2, 3, 5];
@@ -54,15 +50,11 @@ interface NumberArray {
5450
let fibonacci: NumberArray = [1, 1, 2, 3, 5];
5551
```
5652

57-
`NumberArray` 表示:只要 `index` 的类型是 `number`,那么值的类型必须是 `number`
53+
`NumberArray` 表示:只要索引的类型是数字时,那么值的类型必须是数字
5854

59-
## any 在数组中的应用
55+
虽然接口也可以用来描述数组,但是我们一般不会这么做,因为这种方式比前两种方式复杂多了。
6056

61-
一个比较常见的做法是,用 `any` 表示数组中允许出现任意类型:
62-
63-
```ts
64-
let list: any[] = ['Xcat Liu', 25, { website: 'http://xcatliu.com' }];
65-
```
57+
不过有一种情况例外,那就是它常用来表示类数组。
6658

6759
## 类数组
6860

@@ -73,20 +65,51 @@ function sum() {
7365
let args: number[] = arguments;
7466
}
7567

76-
// index.ts(2,7): error TS2322: Type 'IArguments' is not assignable to type 'number[]'.
77-
// Property 'push' is missing in type 'IArguments'.
68+
// Type 'IArguments' is missing the following properties from type 'number[]': pop, push, concat, join, and 24 more.
7869
```
7970

80-
事实上常见的类数组都有自己的接口定义,如 `IArguments`, `NodeList`, `HTMLCollection` 等:
71+
上例中,`arguments` 实际上是一个类数组,不能用普通的数组的方式来描述,而应该用接口:
72+
73+
```ts
74+
function sum() {
75+
let args: {
76+
[index: number]: number;
77+
length: number;
78+
callee: Function;
79+
} = arguments;
80+
}
81+
```
82+
83+
在这个例子中,我们除了约束当索引的类型是数字时,值的类型必须是数字之外,也约束了它还有 `length``callee` 两个属性。
84+
85+
事实上常用的类数组都有自己的接口定义,如 `IArguments`, `NodeList`, `HTMLCollection` 等:
8186

8287
```ts
8388
function sum() {
8489
let args: IArguments = arguments;
8590
}
8691
```
8792

93+
其中 `IArguments` 是 TypeScript 中定义好了的类型,它实际上就是:
94+
95+
```ts
96+
interface IArguments {
97+
[index: number]: any;
98+
length: number;
99+
callee: Function;
100+
}
101+
```
102+
88103
关于内置对象,可以参考[内置对象](./built-in-objects.md)一章。
89104

105+
## any 在数组中的应用
106+
107+
一个比较常见的做法是,用 `any` 表示数组中允许出现任意类型:
108+
109+
```ts
110+
let list: any[] = ['xcatliu', 25, { website: 'http://xcatliu.com' }];
111+
```
112+
90113
## 参考
91114

92115
- [Basic Types # Array](http://www.typescriptlang.org/docs/handbook/basic-types.html#array)[中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html#数组)

examples/test.js

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

examples/test.ts

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

0 commit comments

Comments
 (0)