Skip to content

Commit c0aa2b2

Browse files
committed
Add documentation for the never type
1 parent d9a309b commit c0aa2b2

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

doc/handbook/Basic Types.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ notSure = false; // okay, definitely a boolean
153153

154154
在对现有代码进行改写的时候,`any`类型是十分有用的,它允许你在编译时可选择地包含或移除类型检查。
155155
你可能认为`Object`有相似的作用,就像它在其它语言中那样。
156-
但是`Object`类型的变量只是允许你给它赋任意值 -- 但是却不能够在它上面调用任意的方法,即便它真的有这些方法:
156+
但是`Object`类型的变量只是允许你给它赋任意值 - 但是却不能够在它上面调用任意的方法,即便它真的有这些方法:
157157

158158
```ts
159159
let notSure: any = 4;
@@ -211,6 +211,35 @@ let n: null = null;
211211

212212
> 注意:我们鼓励尽可能地使用`--strictNullChecks`,但在本手册里我们假设这个标记是关闭的。
213213
214+
# Never
215+
216+
`never`类型表示的是那些永不存在的值的类型。
217+
例如,`never`类型是那些总是会抛出异常或根本就不会有返回值的函数表达式或箭头函数表达式的返回值类型;
218+
变量也可能是`never`类型,当它们被永不为真的类型保护所约束时。
219+
220+
`never`类型是任何类型的子类型,也可以赋值给任何类型;然而,*没有*类型是`never`的子类型或可以赋值给`never`类型(除了`never`本身之外)。
221+
即使`any`也不可以赋值给`never`
222+
223+
下面是一些返回`never`类型的函数:
224+
225+
```ts
226+
// 返回never的函数必须存在无法达到的终点
227+
function error(message: string): never {
228+
throw new Error(message);
229+
}
230+
231+
// 推断的返回值类型为never
232+
function fail() {
233+
return error("Something failed");
234+
}
235+
236+
// 返回never的函数必须存在无法达到的终点
237+
function infiniteLoop(): never {
238+
while (true) {
239+
}
240+
}
241+
```
242+
214243
# 类型断言
215244

216245
有时候你会遇到这样的情况,你会比TypeScript更了解某个值的详细信息。

preface.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ TypeScript目前还在积极的开发完善之中,不断地会有新的特性
8585

8686
## 主要修改 (Latest 10 updates)
8787

88+
* 2016-09-26 新增章节:[基础类型 - Never](./doc/handbook/Basic Types.html)
8889
* 2016-08-19 新增章节:[如何书写声明文件](./doc/handbook/declaration files/Introduction.html)
8990
* 2016-07-21 新增章节:[noUnusedLocals和noUnusedParameter编译选项](./doc/handbook/Compiler Options in MSBuild.html)
9091
* 2016-07-21 新增章节:[tsconfig.json文件里的include和exclude匹配](./doc/handbook/tsconfig.json.html)
@@ -94,7 +95,6 @@ TypeScript目前还在积极的开发完善之中,不断地会有新的特性
9495
* 2016-06-15 新增章节:快速上手:[Angular 2](./doc/handbook/tutorials/Angular 2.html)
9596
* 2016-06-11 新增章节:快速上手:[Gulp](./doc/handbook/tutorials/Gulp.html)
9697
* 2016-04-23 新增章节:[使用TypeScript的每日构建版本](./doc/handbook/Nightly Builds.html)
97-
* 2016-04-18 新增章节:[新增功能](./doc/release-notes/README.html)
9898

9999

100100
## 相关链接

0 commit comments

Comments
 (0)