Skip to content

Commit 5e956cd

Browse files
committed
Finish basics
1 parent 0c9ca79 commit 5e956cd

15 files changed

+87
-72
lines changed

SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- [原始数据类型](basics/primitive-data-types.md)
1010
- [任意值(Any)](basics/any.md)
1111
- [类型推论](basics/type-inference.md)
12-
- [联合类型(Union Types)](basics/union-types.md)
12+
- [联合类型](basics/union-types.md)
1313
- [对象的类型——接口](basics/type-of-object-interfaces.md)
1414
- [数组的类型](basics/type-of-array.md)
1515
- [函数的类型](basics/type-of-function.md)

basics/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- [原始数据类型](primitive-data-types.md)
66
- [任意值(Any)](any.md)
77
- [类型推论](type-inference.md)
8-
- [联合类型(Union Types)](union-types.md)
8+
- [联合类型](union-types.md)
99
- [对象的类型——接口](type-of-object-interfaces.md)
1010
- [数组的类型](type-of-array.md)
1111
- [函数的类型](type-of-function.md)

basics/any.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,4 @@ something.setName('Jerry Lee');
6565

6666
## 参考
6767

68-
- [Handbook - Basic Types # Any](http://www.typescriptlang.org/docs/handbook/basic-types.html#any)
69-
- [中文手册 - 基础类型 # 任意值](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html#任意值)
68+
- [Handbook - Basic Types # Any](http://www.typescriptlang.org/docs/handbook/basic-types.html#any) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html#任意值)

basics/built-in-objects.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,15 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven
8888

8989
查看 TypeScript 核心库的定义:[TypeScript 核心库的定义文件]
9090

91-
需要注意的是,TypeScript 核心库的定义中不包含 Node.js 部分。如果要在 Node.js 中使用 TypeScript,可以参考???一章。
91+
需要注意的是,TypeScript 核心库的定义中不包含 Node.js 部分。
92+
93+
## 用 TypeScript 写 Node.js
94+
95+
Node.js 不是内置对象的一部分,如果想用 TypeScript 写 Node.js,则需要引入第三方声明文件:
96+
97+
```shell
98+
npm install @types/node --save-dev
99+
```
92100

93101
## Links
94102

basics/declaration-files.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ jQuery('#foo');
3636
declare var jQuery: (string) => any;
3737
```
3838

39-
> Tip: 我们约定声明文件以 `.d.ts` 为后缀。
39+
> 我们约定声明文件以 `.d.ts` 为后缀。
4040
41-
然后在使用到的文件的开头,用「三斜线指令」表示引用的声明文件
41+
然后在使用到的文件的开头,用「三斜线指令」表示引用了声明文件
4242

4343
```ts
4444
/// <reference path="./jQuery.d.ts" />
@@ -50,15 +50,23 @@ jQuery('#foo');
5050

5151
## 第三方声明文件
5252

53-
当然,jQuery 的声明文件不需要我们定义了,已经有人帮我们定义好了:[jquery.d.ts](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/jquery/jquery.d.ts)
53+
当然,jQuery 的声明文件不需要我们定义了,已经有人帮我们定义好了:[jQuery in DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/jquery)
5454

55-
我们可以下载下来使用,也可以使用 `typing` 方便的获取任何第三方库的声明文件
55+
我们可以直接下载下来使用,但是更推荐的是使用工具统一管理第三方库的声明文件
5656

57-
关于 `typing` 的使用方法,可以参考???一章。
57+
社区已经有多种方式引入声明文件,不过 [TypeScript 2.0 推荐使用 @types 来管理](https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/)
58+
59+
@types 的使用方式很简单,直接用 npm 安装对应的声明模块即可,以 jQuery 举例:
60+
61+
```shell
62+
npm install @types/jquery --save-dev
63+
```
64+
65+
可以在[这个页面](http://microsoft.github.io/TypeSearch/)搜索你需要的声明文件。
66+
67+
更多关于 @types 的使用方法,可以参考???一章。
5868

5969
## Links
6070

61-
- [Handbook - Writing Declaration Files](http://www.typescriptlang.org/docs/handbook/writing-declaration-files.html)
62-
- [中文手册 - 书写 d.ts 文件](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Writing%20Definition%20Files.html)
63-
- [Handbook - Triple-Slash Directives](http://www.typescriptlang.org/docs/handbook/triple-slash-directives.html)
64-
- [中文手册 - 三斜线指令](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Triple-Slash%20Directives.html)
71+
- [Handbook - Writing Declaration Files](http://www.typescriptlang.org/docs/handbook/writing-declaration-files.html) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Writing%20Definition%20Files.html)
72+
- [Handbook - Triple-Slash Directives](http://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Triple-Slash%20Directives.html)

basics/primitive-data-types.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ let num: number = u;
145145

146146
## 参考
147147

148-
- [Handbook - Basic Types](http://www.typescriptlang.org/docs/handbook/basic-types.html)
149-
- [中文手册 - 基础类型](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html)
148+
- [Handbook - Basic Types](http://www.typescriptlang.org/docs/handbook/basic-types.html) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html)
150149
- [Primitive data types][]
151150
- [ES2015 中的新类型 `Symbol`][]
152151
- [ES2015 中的二进制和八进制表示法][]

basics/type-inference.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 类型推论
22

3-
如果没有明确的指定类型,那么 TypeScript 会依照类型推论的规则推断出一个类型
3+
如果没有明确的指定类型,那么 TypeScript 会依照类型推论(Type Inference)的规则推断出一个类型
44

55
## 什么是类型推论
66

@@ -32,7 +32,6 @@ myFavoriteNumber = 'seven';
3232
myFavoriteNumber = 7;
3333
```
3434

35-
## Links
35+
## 参考
3636

37-
- [Handbook - Type Inference](http://www.typescriptlang.org/docs/handbook/type-inference.html)
38-
- [中文手册 - 类型推论](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Type%20Inference.html)
37+
- [Handbook - Type Inference](http://www.typescriptlang.org/docs/handbook/type-inference.html) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Type%20Inference.html)

basics/type-of-array.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# 数组的类型
22

3-
数组类型有多种定义方式,比较灵活。
3+
在 TypeScript 中,数组类型有多种定义方式,比较灵活。
44

5-
## 「类型+方括号」表示法
5+
## 「类型 + 方括号」表示法
66

7-
最简单的方法是使用「类型+方括号」来表示数组:
7+
最简单的方法是使用「类型 + 方括号」来表示数组:
88

99
```ts
1010
let fibonacci: number[] = [1, 1, 2, 3, 5];
@@ -81,16 +81,9 @@ function sum() {
8181
}
8282
```
8383

84-
> Tip: 一般我们使用 `Array.prototype.slice.call(argumants)``[].slice.call(argumants)` 来将一个类数组转化为数组。ES6 中可以使用 [Array.from][ES6 中的 Array.from] 或者 [rest 参数][ES6 中的 rest 参数]实现类似功能
84+
关于内建类型,可以参考[内置对象](./built-in-objects.md)一章
8585

8686
## Links
8787

88-
- [Handbook - Basic Types # Array](http://www.typescriptlang.org/docs/handbook/basic-types.html#array)
89-
- [Handbook - Interfaces # Indexable Types](http://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types)
90-
- [中文手册 - 基础类型 # 数组](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html#数组)
91-
- [中文手册 - 接口 # 数组类型](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Interfaces.html#数组类型)
92-
- [ES6 中的 Array.from]
93-
- [ES6 中的 rest 参数]
94-
95-
[ES6 中的 Array.from]: http://es6.ruanyifeng.com/#docs/array#Array-from
96-
[ES6 中的 rest 参数]: http://es6.ruanyifeng.com/#docs/function#rest参数
88+
- [Handbook - 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#数组)
89+
- [Handbook - Interfaces # Indexable Types](http://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Interfaces.html#数组类型)

basics/type-of-function.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 函数的类型
22

3-
> [函数是 JavaScript 中的一等公民](https://llh911001.gitbooks.io/mostly-adequate-guide-chinese/content/ch2.html)
3+
> [函数是 JavaScript 中的一等公民](https://llh911001.gitbooks.io/mostly-adequate-guide-chinese/content/ch2.html)
44
55
## 函数声明(Function Declaration)
66

@@ -66,11 +66,11 @@ let mySum: (x: number, y: number) => number = function (x: number, y: number): n
6666

6767
在 TypeScript 的类型定义中,`=>` 用来表示函数的定义,左边是输入类型,需要用括号括起来,右边是输出类型。
6868

69-
> Tip: `=>` 在 ES6 中叫箭头函数,应用十分广泛,可以参考 [ES6 中的箭头函数]
69+
其中,`=>` 在 ES6 中叫箭头函数,应用十分广泛,可以参考 [ES6 中的箭头函数]
7070

7171
## 接口中函数的定义
7272

73-
在接口中,需要这样定义一个函数
73+
我们也可以使用接口的方式来定义一个函数需要符合的形状
7474

7575
```ts
7676
interface SearchFunc {
@@ -92,7 +92,7 @@ mySearch = function(source: string, subString: string) {
9292
```ts
9393
function buildName(firstName: string, lastName?: string) {
9494
if (lastName) {
95-
return firstName + " " + lastName;
95+
return firstName + ' ' + lastName;
9696
} else {
9797
return firstName;
9898
}
@@ -106,7 +106,7 @@ let xcat = buildName('Xcat');
106106
```ts
107107
function buildName(firstName?: string, lastName: string) {
108108
if (firstName) {
109-
return firstName + " " + lastName;
109+
return firstName + ' ' + lastName;
110110
} else {
111111
return lastName;
112112
}
@@ -123,7 +123,7 @@ let xcat = buildName(undefined, 'Xcat');
123123

124124
```ts
125125
function buildName(firstName: string, lastName: string = 'Liu') {
126-
return firstName + " " + lastName;
126+
return firstName + ' ' + lastName;
127127
}
128128
let xcatliu = buildName('Xcat', 'Liu');
129129
let xcat = buildName('Xcat');
@@ -133,7 +133,7 @@ let xcat = buildName('Xcat');
133133

134134
```ts
135135
function buildName(firstName: string = 'Xcat', lastName: string) {
136-
return firstName + " " + lastName;
136+
return firstName + ' ' + lastName;
137137
}
138138
let xcatliu = buildName('Xcat', 'Liu');
139139
let xcat = buildName(undefined, 'Xcat');
@@ -169,14 +169,12 @@ let a = [];
169169
push(a, 1, 2, 3);
170170
```
171171

172-
> Tip: 注意 rest 参数只能是最后一个参数,关于 rest 参数,可以参考 [ES6 中的 rest 参数]
172+
注意,rest 参数只能是最后一个参数,关于 rest 参数,可以参考 [ES6 中的 rest 参数]
173173

174-
## Links
174+
## 参考
175175

176-
- [Handbook - Functions](http://www.typescriptlang.org/docs/handbook/functions.html)
177-
- [Handbook - Functions # Function Types](http://www.typescriptlang.org/docs/handbook/interfaces.html#function-types)
178-
- [中文手册 - 函数](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Functions.html)
179-
- [中文手册 - 接口 # 函数类型](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Interfaces.html#函数类型)
176+
- [Handbook - Functions](http://www.typescriptlang.org/docs/handbook/functions.html) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Functions.html)
177+
- [Handbook - Functions # Function Types](http://www.typescriptlang.org/docs/handbook/interfaces.html#function-types) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Interfaces.html#函数类型)
180178
- [JS 函数式编程指南](https://llh911001.gitbooks.io/mostly-adequate-guide-chinese/content/)
181179
- [ES6 中的箭头函数]
182180
- [ES6 中函数参数的默认值]

basics/type-of-object-interfaces.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# 对象的类型——接口
22

3-
使用接口(Interfaces)来定义对象的类型。
3+
在 TypeScript 中,我们使用接口(Interfaces)来定义对象的类型。
44

55
## 什么是接口(Interfaces)
66

77
在面向对象语言中,接口(Interfaces)是一个很重要的概念,它是对行为的抽象,而具体如何行动需要由类(classes)去实现(implements)。
88

9-
TypeScript 中的接口是一个非常灵活的概念,除了可用于[对类的一部分行为进行抽象](../advanced/use-of-class.md#实现接口)以外,也常用于对「对象的形状(Shape)」进行描述,想了解接口的所有用法,可以参考???
9+
TypeScript 中的接口是一个非常灵活的概念,除了可用于[对类的一部分行为进行抽象](../advanced/use-of-class.md#实现接口)以外,也常用于对「对象的形状(Shape)」进行描述。
1010

1111
## 简单的例子
1212

@@ -24,7 +24,7 @@ let xcatliu: Person = {
2424

2525
上面的例子中,我们定义了一个接口 `Person`,接着定义了一个变量 `xcatliu`,它的类型是 `Person`。这样,我们就约束了 `xcatliu` 的形状必须和接口 `Person` 一致。
2626

27-
> Tip: 接口的定义一般首字母大写
27+
接口一般首字母大写
2828

2929
如果定义的变量比接口少了一些属性会怎么样呢?
3030

@@ -105,8 +105,8 @@ let xcatliu: Person = {
105105
website: 'http://xcatliu.com',
106106
};
107107

108-
// index.ts(9,3): error TS2322: Type '{ name: string; age: number; github: string; }' is not assignable to type 'Person'.
109-
// Object literal may only specify known properties, and 'github' does not exist in type 'Person'
108+
// examples/playground/index.ts(9,3): error TS2322: Type '{ name: string; age: number; website: string; }' is not assignable to type 'Person'.
109+
// Object literal may only specify known properties, and 'website' does not exist in type 'Person'.
110110
```
111111

112112
由此可见,即使有可选属性,也不可以添加未定义的属性。
@@ -156,7 +156,6 @@ let xcatliu: Person = {
156156

157157
另外,在报错信息中可以看出,此时 `{ name: 'Xcat Liu', age: 25, website: 'http://xcatliu.com' }` 的类型被推断成了 `{ [x: string]: string | number; name: string; age: number; github: string; }`,这是联合类型和接口的结合。
158158

159-
## Links
159+
## 参考
160160

161-
- [Handbook - Interfaces](http://www.typescriptlang.org/docs/handbook/interfaces.html)
162-
- [中文手册 - 接口](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Interfaces.html)
161+
- [Handbook - Interfaces](http://www.typescriptlang.org/docs/handbook/interfaces.html) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Interfaces.html)

basics/union-types.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
# 联合类型(Union Types)
1+
# 联合类型
22

3-
联合类型表示支持多种类型
3+
联合类型(Union Types)表示取值可以为多种类型
44

55
## 简单的例子
66

77
```ts
8-
let myFavoriteNumber: string | number = 'seven';
9-
myFavoriteNumber = 25;
8+
let myFavoriteNumber: string | number;
9+
myFavoriteNumber = 'seven';
10+
myFavoriteNumber = 7;
1011
```
1112

1213
```ts
@@ -21,7 +22,7 @@ myFavoriteNumber = true;
2122

2223
## 访问联合类型的属性或方法
2324

24-
如果一个值是联合类型,我们只能访问此联合类型的所有类型里共有的属性或方法
25+
如果一个值是联合类型,我们**只能访问此联合类型的所有类型里共有的属性或方法**
2526

2627
```ts
2728
let myFavoriteNumber: string | number = 'seven';
@@ -39,5 +40,4 @@ myFavoriteNumber.toString();
3940

4041
## Links
4142

42-
- [Handbook - Advanced Types # Union Types](http://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types)
43-
- [中文手册 - 高级类型 # 联合类型](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Advanced%20Types.html#联合类型)
43+
- [Handbook - Advanced Types # Union Types](http://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Advanced%20Types.html#联合类型)

examples/playground/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var myName = 'Xcat Liu';
2-
var myAge = 25;
3-
// 模板字符串
4-
var sentence = "Hello, my name is " + myName + ".\nI'll be " + (myAge + 1) + " years old next month.";
1+
var xcatliu = {
2+
name: 'Xcat Liu',
3+
age: 25,
4+
website: 'http://xcatliu.com'
5+
};

examples/playground/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
let myName: string = 'Xcat Liu';
2-
let myAge: number = 25;
3-
4-
// 模板字符串
5-
let sentence: string = `Hello, my name is ${myName}.
6-
I'll be ${myAge + 1} years old next month.`;
1+
document.addEventListener('click', function(e) {
2+
console.log(e.targetCurrent);
3+
});

examples/playground/jQuery.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/playground/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "playground",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"devDependencies": {
12+
"@types/jquery": "^2.0.39",
13+
"@types/node": "^7.0.0"
14+
}
15+
}

0 commit comments

Comments
 (0)