Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
165 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Never 类型 | ||
|
||
`never` 表示永远不存在值的类型,一般用于错误处理函数: | ||
|
||
```ts | ||
// 返回 never 的函数必须存在无法达到的终点 | ||
function error(message: string): never { | ||
throw new Error(message); | ||
} | ||
``` | ||
|
||
## 参考 | ||
|
||
- [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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
var directions = [0 /* Up */, 1 /* Down */, 2 /* Left */, 3 /* Right */]; | ||
function sayHello(person) { | ||
return 'Hello, ' + person; | ||
} | ||
var user = [0, 1, 2]; | ||
document.body.innerHTML = sayHello(user); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
declare const enum Directions { | ||
Up, | ||
Down, | ||
Left, | ||
Right | ||
function sayHello(person: string) { | ||
return 'Hello, ' + person; | ||
} | ||
|
||
let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right]; | ||
let user = [0, 1, 2]; | ||
document.body.innerHTML = sayHello(user); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
# 简介 | ||
|
||
本部分介绍了在学习 TypeScript 之前需要了解的知识,具体包括: | ||
本部分介绍了在学习 TypeScript 之前需要了解的知识,具体内容包括: | ||
|
||
- [什么是 TypeScript](what-is-typescript.md) | ||
- [安装 TypeScript] | ||
- [Hello World] | ||
- [安装 TypeScript](get-typescript.md) | ||
- [Hello TypeScript](hello-typescript.md) | ||
|
||
--- | ||
|
||
- [上一章:前言](../README.md) | ||
- [下一章:什么是 TypeScript](what-is-typescript.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 安装 TypeScript | ||
|
||
TypeScript 的命令行工具安装方法如下: | ||
|
||
```shell | ||
npm install -g typescript | ||
``` | ||
|
||
安装完成之后,就有了 `tsc` 命令。编译一个 TypeScript 文件很简单: | ||
|
||
```shell | ||
tsc hello.ts | ||
``` | ||
|
||
我们约定使用 TypeScript 编写的文件以 `.ts` 为后缀。 | ||
|
||
## 编辑器 | ||
|
||
TypeScript 最大的优势之一便是增强了编辑器和 IDE 的功能,包括代码补全、接口提示、跳转到定义、重构等。 | ||
|
||
主流的编辑器都支持 TypeScript,这里我推荐使用 [Visual Studio Code](https://code.visualstudio.com/)。 | ||
|
||
它是一款开源,跨终端的轻量级编辑器,内置了 TypeScript 支持。 | ||
|
||
另外它本身也是[用 TypeScript 编写的](https://github.com/Microsoft/vscode/)。 | ||
|
||
下载安装:https://code.visualstudio.com/ | ||
|
||
获取其他编辑器或 IDE 对 TypeScript 的支持: | ||
|
||
- [Sublime Text](https://github.com/Microsoft/TypeScript-Sublime-Plugin) | ||
- [Atom](https://atom.io/packages/atom-typescript) | ||
- [WebStorm](https://www.jetbrains.com/webstorm/) | ||
- [Vim](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Editor-Support#vim) | ||
- [Emacs](https://github.com/ananthakumaran/tide) | ||
- [Eclipse](https://github.com/palantir/eclipse-typescript) | ||
- [Visual Studio 2015](https://www.microsoft.com/en-us/download/details.aspx?id=48593) | ||
- [Visual Studio 2013](https://www.microsoft.com/en-us/download/details.aspx?id=48739) | ||
|
||
--- | ||
|
||
- [上一章:什么是 TypeScript](what-is-typescript.md) | ||
- [下一章:Hello TypeScript](hello-typescript.md) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Hello TypeScript | ||
|
||
我们从一个简单的例子开始。 | ||
|
||
将以下代码复制到 `hello.ts` 中: | ||
|
||
```ts | ||
function sayHello(person: string) { | ||
return 'Hello, ' + person; | ||
} | ||
|
||
let user = 'Xcat Liu'; | ||
document.body.innerHTML = sayHello(user); | ||
``` | ||
|
||
然后执行 | ||
|
||
```shell | ||
tsc hello.ts | ||
``` | ||
|
||
这时候会生成一个编译好的文件 `hello.js`: | ||
|
||
```js | ||
function sayHello(person) { | ||
return 'Hello, ' + person; | ||
} | ||
var user = 'Xcat Liu'; | ||
document.body.innerHTML = sayHello(user); | ||
``` | ||
|
||
TypeScript 中,使用 `:` 指定变量的类型,`:` 的前后有没有空格都可以。 | ||
|
||
上述例子中,我们用 `:` 指定 `person` 参数类型为 `string`。但是编译为 js 之后,并没有什么检查的代码被插入进来。 | ||
|
||
**TypeScript 只会进行静态检查,如果发现有错误,编译的时候就会报错。** | ||
|
||
> Tip: `let` 是 ES6 中的关键字,和 `var` 类似,用于定义一个局部变量,可以参阅 [let 和 const 命令](http://es6.ruanyifeng.com/#docs/let)。 | ||
下面尝试把这段代码编译一下: | ||
|
||
```ts | ||
function sayHello(person: string) { | ||
return 'Hello, ' + person; | ||
} | ||
|
||
let user = [0, 1, 2]; | ||
document.body.innerHTML = sayHello(user); | ||
``` | ||
|
||
编辑器中会提示错误,编译的时候也会出错: | ||
|
||
```shell | ||
index.ts(6,36): error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'string'. | ||
``` | ||
|
||
但是还是生成了 js 文件: | ||
|
||
```js | ||
function sayHello(person) { | ||
return 'Hello, ' + person; | ||
} | ||
var user = [0, 1, 2]; | ||
document.body.innerHTML = sayHello(user); | ||
``` | ||
|
||
TypeScript 编译的时候即使报错了,还是会生成编译结果,我们仍然可以使用这个编译之后的文件。 | ||
|
||
如果要在报错的时候终止 js 文件的生成,可以在 `tsconfig.json` 中配置 ??? 即可。关于 `tsconfig.json`,请参阅《???》一章。 | ||
|
||
--- | ||
|
||
- [上一章:安装 TypeScript](get-typescript.md) | ||
- [下一章:基础](../basics/README.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters