Skip to content

Commit 43a4961

Browse files
committed
Complete introduction
1 parent bcf06e7 commit 43a4961

File tree

11 files changed

+165
-105
lines changed

11 files changed

+165
-105
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
77
## 关于本书
88

9-
- [在线阅读](https://ts.xcatliu.com/)(部署在 GitBook 上,需要翻墙)
10-
- [在线阅读(GitHub 版)](basics/README.md)
9+
- [在线阅读](https://ts.xcatliu.com/)(部署在 [GitBook](https://www.gitbook.com/book/xcatliu/typescript-tutorial/details) 上,需要翻墙)
10+
- [在线阅读(GitHub 版)](introduction/README.md)
1111
- [GitHub 地址][GitHub]
1212
- 作者:[xcatliu](https://github.com/xcatliu/)
1313

1414
本书是作者在学习 [TypeScript] 后总结的入门教程。
1515

1616
随着对 TypeScript 理解的加深和 TypeScript 社区的发展,本书也会做出相应的更新,欢迎大家 [Star 收藏][GitHub]
1717

18-
- 在线版托管在 [GitBook](https://www.gitbook.com/book/xcatliu/typescript-tutorial/details)
1918
- 发现文章内容有问题,可以直接在页面下方评论
2019
- 对项目的建议,可以[提交 issue](https://github.com/xcatliu/typescript-tutorial/issues/new) 向作者反馈
2120
- 欢迎直接提交 pull-request 参与贡献
@@ -26,6 +25,8 @@ TypeScript 虽然有[官方手册][Handbook]及其[非官方中文版][中文手
2625

2726
与官方手册不同,本书着重于从 JavaScript 程序员的角度总结思考,循序渐进的理解 TypeScript,希望能给大家一些帮助和启示。
2827

28+
由于一些知识点与官方手册重合度很高,本书会在相应章节推荐直接阅读中文手册。
29+
2930
## 关于 TypeScript
3031

3132
[TypeScript] 是 JavaScript 的一个超集,主要提供了**类型系统****对 ES6 的支持**,它由 Microsoft 开发,代码[开源于 GitHub](https://github.com/Microsoft/TypeScript) 上。
@@ -39,11 +40,12 @@ TypeScript 虽然有[官方手册][Handbook]及其[非官方中文版][中文手
3940
- 熟悉 JavaScript,至少阅读过一遍[《JavaScript 高级程序设计》](https://book.douban.com/subject/10546125/)
4041
- 了解 ES6,推荐阅读 [ECMAScript 6 入门]
4142
- 了解 Node.js,会用 npm 安装及使用一些工具
42-
- 想学习 TypeScript 或者想对 TypeScript 有更深的理解
43+
- 想了解 TypeScript 或者想对 TypeScript 有更深的理解
4344

4445
本书**不适合**以下人群
4546

4647
- 没有系统学习过 JavaScript
48+
- 已经能够很熟练的运用 TypeScript
4749

4850
## 版权许可
4951

@@ -59,6 +61,10 @@ TypeScript 虽然有[官方手册][Handbook]及其[非官方中文版][中文手
5961
- [Handbook] | [中文版][中文手册]
6062
- [ECMAScript 6 入门]
6163

64+
---
65+
66+
- [下一章:简介](introduction/README.md)
67+
6268
[GitHub]: https://github.com/xcatliu/typescript-tutorial
6369
[TypeScript]: http://www.typescriptlang.org/
6470
[Handbook]: http://www.typescriptlang.org/docs/handbook/basic-types.html

SUMMARY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
- [前言](README.md)
44
- [简介](introduction/README.md)
55
- [什么是 TypeScript](introduction/what-is-typescript.md)
6-
- 安装 TypeScript(未完成)
7-
- Helo World(未完成)
6+
- [安装 TypeScript](introduction/get-typescript.md)
7+
- [Hello TypeScript](introduction/hello-typescript.md)
88
- [基础](basics/README.md)
99
- [原始数据类型](basics/primitive-data-types.md)
1010
- [任意值(Any)](basics/any.md)

advanced/README.md

Whitespace-only changes.

advanced/never.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Never 类型
2+
3+
`never` 表示永远不存在值的类型,一般用于错误处理函数:
4+
5+
```ts
6+
// 返回 never 的函数必须存在无法达到的终点
7+
function error(message: string): never {
8+
throw new Error(message);
9+
}
10+
```
11+
12+
## 参考
13+
14+
- [Handbook - Basic Types # Never](http://www.typescriptlang.org/docs/handbook/basic-types.html#never) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html#never)

examples/playground/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
var directions = [0 /* Up */, 1 /* Down */, 2 /* Left */, 3 /* Right */];
1+
function sayHello(person) {
2+
return 'Hello, ' + person;
3+
}
4+
var user = [0, 1, 2];
5+
document.body.innerHTML = sayHello(user);

examples/playground/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
declare const enum Directions {
2-
Up,
3-
Down,
4-
Left,
5-
Right
1+
function sayHello(person: string) {
2+
return 'Hello, ' + person;
63
}
74

8-
let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right];
5+
let user = [0, 1, 2];
6+
document.body.innerHTML = sayHello(user);

introduction/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# 简介
22

3-
本部分介绍了在学习 TypeScript 之前需要了解的知识,具体包括
3+
本部分介绍了在学习 TypeScript 之前需要了解的知识,具体内容包括
44

55
- [什么是 TypeScript](what-is-typescript.md)
6-
- [安装 TypeScript]
7-
- [Hello World]
6+
- [安装 TypeScript](get-typescript.md)
7+
- [Hello TypeScript](hello-typescript.md)
8+
9+
---
10+
11+
- [上一章:前言](../README.md)
12+
- [下一章:什么是 TypeScript](what-is-typescript.md)

introduction/get-typescript.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 安装 TypeScript
2+
3+
TypeScript 的命令行工具安装方法如下:
4+
5+
```shell
6+
npm install -g typescript
7+
```
8+
9+
安装完成之后,就有了 `tsc` 命令。编译一个 TypeScript 文件很简单:
10+
11+
```shell
12+
tsc hello.ts
13+
```
14+
15+
我们约定使用 TypeScript 编写的文件以 `.ts` 为后缀。
16+
17+
## 编辑器
18+
19+
TypeScript 最大的优势之一便是增强了编辑器和 IDE 的功能,包括代码补全、接口提示、跳转到定义、重构等。
20+
21+
主流的编辑器都支持 TypeScript,这里我推荐使用 [Visual Studio Code](https://code.visualstudio.com/)
22+
23+
它是一款开源,跨终端的轻量级编辑器,内置了 TypeScript 支持。
24+
25+
另外它本身也是[用 TypeScript 编写的](https://github.com/Microsoft/vscode/)
26+
27+
下载安装:https://code.visualstudio.com/
28+
29+
获取其他编辑器或 IDE 对 TypeScript 的支持:
30+
31+
- [Sublime Text](https://github.com/Microsoft/TypeScript-Sublime-Plugin)
32+
- [Atom](https://atom.io/packages/atom-typescript)
33+
- [WebStorm](https://www.jetbrains.com/webstorm/)
34+
- [Vim](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Editor-Support#vim)
35+
- [Emacs](https://github.com/ananthakumaran/tide)
36+
- [Eclipse](https://github.com/palantir/eclipse-typescript)
37+
- [Visual Studio 2015](https://www.microsoft.com/en-us/download/details.aspx?id=48593)
38+
- [Visual Studio 2013](https://www.microsoft.com/en-us/download/details.aspx?id=48739)
39+
40+
---
41+
42+
- [上一章:什么是 TypeScript](what-is-typescript.md)
43+
- [下一章:Hello TypeScript](hello-typescript.md)

introduction/greeter.md

Lines changed: 0 additions & 89 deletions
This file was deleted.

introduction/hello-typescript.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Hello TypeScript
2+
3+
我们从一个简单的例子开始。
4+
5+
将以下代码复制到 `hello.ts` 中:
6+
7+
```ts
8+
function sayHello(person: string) {
9+
return 'Hello, ' + person;
10+
}
11+
12+
let user = 'Xcat Liu';
13+
document.body.innerHTML = sayHello(user);
14+
```
15+
16+
然后执行
17+
18+
```shell
19+
tsc hello.ts
20+
```
21+
22+
这时候会生成一个编译好的文件 `hello.js`
23+
24+
```js
25+
function sayHello(person) {
26+
return 'Hello, ' + person;
27+
}
28+
var user = 'Xcat Liu';
29+
document.body.innerHTML = sayHello(user);
30+
```
31+
32+
TypeScript 中,使用 `:` 指定变量的类型,`:` 的前后有没有空格都可以。
33+
34+
上述例子中,我们用 `:` 指定 `person` 参数类型为 `string`。但是编译为 js 之后,并没有什么检查的代码被插入进来。
35+
36+
**TypeScript 只会进行静态检查,如果发现有错误,编译的时候就会报错。**
37+
38+
> Tip: `let` 是 ES6 中的关键字,和 `var` 类似,用于定义一个局部变量,可以参阅 [let 和 const 命令](http://es6.ruanyifeng.com/#docs/let)
39+
40+
下面尝试把这段代码编译一下:
41+
42+
```ts
43+
function sayHello(person: string) {
44+
return 'Hello, ' + person;
45+
}
46+
47+
let user = [0, 1, 2];
48+
document.body.innerHTML = sayHello(user);
49+
```
50+
51+
编辑器中会提示错误,编译的时候也会出错:
52+
53+
```shell
54+
index.ts(6,36): error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'string'.
55+
```
56+
57+
但是还是生成了 js 文件:
58+
59+
```js
60+
function sayHello(person) {
61+
return 'Hello, ' + person;
62+
}
63+
var user = [0, 1, 2];
64+
document.body.innerHTML = sayHello(user);
65+
```
66+
67+
TypeScript 编译的时候即使报错了,还是会生成编译结果,我们仍然可以使用这个编译之后的文件。
68+
69+
如果要在报错的时候终止 js 文件的生成,可以在 `tsconfig.json` 中配置 ??? 即可。关于 `tsconfig.json`,请参阅《???》一章。
70+
71+
---
72+
73+
- [上一章:安装 TypeScript](get-typescript.md)
74+
- [下一章:基础](../basics/README.md)

introduction/what-is-typescript.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@
4949
大家可以根据自己团队和项目的情况判断是否需要使用 TypeScript。
5050

5151
[TypeScript]: http://www.typescriptlang.org/
52+
53+
---
54+
55+
- [上一章:简介](README.md)
56+
- [下一章:安装 TypeScript](get-typescript.md)

0 commit comments

Comments
 (0)