Skip to content

Commit dfdb198

Browse files
committed
Fix all the TBDs in basics
1 parent 866f80f commit dfdb198

File tree

13 files changed

+244
-81
lines changed

13 files changed

+244
-81
lines changed

advanced/further-reading.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- [Never](http://www.typescriptlang.org/docs/handbook/basic-types.html#never)[中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html#never)):永远不存在值的类型,一般用于错误处理函数
88
- [变量声明](http://www.typescriptlang.org/docs/handbook/variable-declarations.html)[中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Variable%20Declarations.html)):使用 `let``const` 替代 `var`,这是 [ES6 的知识](http://es6.ruanyifeng.com/#docs/let)
9+
- [`this`](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Functions.html#this)
910
- [泛型#在泛型约束中使用类型参数](http://www.typescriptlang.org/docs/handbook/generics.html#using-type-parameters-in-generic-constraints)[中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Generics.html#在泛型约束中使用类型参数)):多个类型参数间可以互相约束
1011
- [泛型#在泛型里使用类类型](http://www.typescriptlang.org/docs/handbook/generics.html#using-class-types-in-generics)[中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Generics.html#在泛型里使用类类型)):创建工厂函数时,需要引用构造函数的类类型
1112
- [类型推论#最佳通用类型](http://www.typescriptlang.org/docs/handbook/type-inference.html#best-common-type)[中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Type%20Inference.html#最佳通用类型)):数组的类型推论

basics/any.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ anyThing.setName('Jerry Lee').sayHello();
3939
anyThing.myName.setFirstName('Cat');
4040
```
4141

42-
可以认为,声明一个变量为任意值之后,对它的任何操作,返回的内容的类型都是任意值。
42+
可以认为,**声明一个变量为任意值之后,对它的任何操作,返回的内容的类型都是任意值**
4343

4444
## 未声明类型的变量
4545

46-
变量如果在声明的时候,未指定其类型,那么它会被识别为任意值类型:
46+
**变量如果在声明的时候,未指定其类型,那么它会被识别为任意值类型**
4747

4848
```ts
4949
let something;
@@ -65,7 +65,7 @@ something.setName('Jerry Lee');
6565

6666
## 参考
6767

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#任意值)
68+
- [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#任意值)
6969

7070
---
7171

basics/built-in-objects.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 内置对象
22

3-
JavaScript 中有很多[内置对象],它们可以直接在 TypeScript 中当做定义好了的类型。
3+
JavaScript 中有很多[内置对象][],它们可以直接在 TypeScript 中当做定义好了的类型。
44

55
内置对象是指根据标准在全局作用域(Global)上存在的对象。这里的标准是指 ECMAScript 和其他环境(比如 DOM)的标准。
66

@@ -21,7 +21,7 @@ let r: RegExp = /[a-z]/;
2121

2222
更多的内置对象,可以查看 [MDN 的文档][内置对象]
2323

24-
而他们的定义文件,则在 [TypeScript 核心库的定义文件]中。
24+
而他们的定义文件,则在 [TypeScript 核心库的定义文件][]中。
2525

2626
## DOM 和 BOM 的内置对象
2727

@@ -39,11 +39,11 @@ document.addEventListener('click', function(e: MouseEvent) {
3939
});
4040
```
4141

42-
它们的定义文件同样在 [TypeScript 核心库的定义文件]中。
42+
它们的定义文件同样在 [TypeScript 核心库的定义文件][]中。
4343

4444
## TypeScript 核心库的定义文件
4545

46-
[TypeScript 核心库的定义文件]中定义了所有浏览器环境需要用到的类型,并且是预置在 TypeScript 中的。
46+
[TypeScript 核心库的定义文件][]中定义了所有浏览器环境需要用到的类型,并且是预置在 TypeScript 中的。
4747

4848
当你在使用一些常用的方法的时候,TypeScript 实际上已经帮你做了很多类型判断的工作了,比如:
4949

@@ -86,9 +86,7 @@ interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEven
8686

8787
所以 `e` 被推断成了 `MouseEvent`,而 `MouseEvent` 是没有 `targetCurrent` 属性的,所以报错了。
8888

89-
查看 TypeScript 核心库的定义:[TypeScript 核心库的定义文件]
90-
91-
需要注意的是,TypeScript 核心库的定义中不包含 Node.js 部分。
89+
注意,TypeScript 核心库的定义中不包含 Node.js 部分。
9290

9391
## 用 TypeScript 写 Node.js
9492

@@ -100,8 +98,8 @@ npm install @types/node --save-dev
10098

10199
## Links
102100

103-
- [内置对象]
104-
- [TypeScript 核心库的定义文件]
101+
- [内置对象][]
102+
- [TypeScript 核心库的定义文件][]
105103

106104
[内置对象]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
107105
[TypeScript 核心库的定义文件]: https://github.com/Microsoft/TypeScript/tree/master/src/lib

basics/declaration-files.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ npm install @types/jquery --save-dev
7272

7373
可以在[这个页面](http://microsoft.github.io/TypeSearch/)搜索你需要的声明文件。
7474

75-
更多关于 @types 的使用方法,可以参考???一章。
76-
7775
## Links
7876

79-
- [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)
80-
- [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)
77+
- [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)
78+
- [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)
8179

8280
---
8381

basics/primitive-data-types.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ JavaScript 的类型分为两种:原始数据类型([Primitive data types][]
66

77
本节主要介绍**前五种**原始数据类型在 TypeScript 中的应用。
88

9-
## 布尔值(Boolean)
9+
## 布尔值
1010

1111
布尔值是最基础的数据类型,在 TypeScript 中,使用 `boolean` 定义布尔值类型:
1212

@@ -17,7 +17,7 @@ let isDone: boolean = false;
1717
// 后面约定,未强调编译错误的代码片段,默认为编译通过
1818
```
1919

20-
注意,使用构造函数 `Boolean` 创造的对象不是布尔值
20+
注意,使用构造函数 `Boolean` 创造的对象**不是**布尔值
2121

2222
```ts
2323
let createdByNewBoolean: boolean = new Boolean(1);
@@ -40,7 +40,7 @@ let createdByBoolean: boolean = Boolean(1);
4040

4141
在 TypeScript 中,`boolean` 是 JavaScript 中的基本类型,而 `Boolean` 是 JavaScript 中的构造函数。其他基本类型(除了 `null``undefined`)一样,不再赘述。
4242

43-
## 数值(Number)
43+
## 数值
4444

4545
使用 `number` 定义数值类型:
4646

@@ -70,7 +70,7 @@ var infinityNumber = Infinity;
7070

7171
其中 `0b1010``0o744`[ES6 中的二进制和八进制表示法][],它们会被编译为十进制数字。
7272

73-
## 字符串(String)
73+
## 字符串
7474

7575
使用 `string` 定义字符串类型:
7676

@@ -94,17 +94,17 @@ var sentence = "Hello, my name is " + myName + ".\nI'll be " + (myAge + 1) + " y
9494

9595
其中 <code>&#96;</code> 用来定义 [ES6 中的模板字符串][]`${expr}` 用来在模板字符串中嵌入表达式。
9696

97-
## 空值(Void)
97+
## 空值
9898

99-
空值 `void` 一般用于没有任何返回值的函数
99+
JavaScript 没有空值(Void)的概念,在 TypeScirpt 中,可以用 `void` 表示没有任何返回值的函数
100100

101101
```ts
102102
function alertName(): void {
103103
alert('My name is xcatliu');
104104
}
105105
```
106106

107-
声明一个 `void` 类型的变量没有什么大用,因为你只能将它赋值为 `undefined``null`
107+
声明一个 `void` 类型的变量没有什么用,因为你只能将它赋值为 `undefined``null`
108108

109109
```ts
110110
let unusable: void = undefined;
@@ -145,7 +145,7 @@ let num: number = u;
145145

146146
## 参考
147147

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)
148+
- [Basic Types](http://www.typescriptlang.org/docs/handbook/basic-types.html) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html)
149149
- [Primitive data types][]
150150
- [ES6 中的新类型 `Symbol`][]
151151
- [ES6 中的二进制和八进制表示法][]

basics/type-assertion.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,67 @@
11
# 类型断言
22

3-
TBD
3+
类型断言(Type Assertion)用来手动将一个联合类型的变量指定为一个更加具体的类型。
4+
5+
## 简单的例子
6+
7+
[之前提到过](union-types.md#访问联合类型的属性或方法),当 TypeScript 不确定一个联合类型的变量到底是哪个类型的时候,我们**只能访问此联合类型的所有类型里共有的属性或方法**
8+
9+
```ts
10+
function getLength(something: string | number): number {
11+
return something.length;
12+
}
13+
14+
// index.ts(2,20): error TS2339: Property 'length' does not exist on type 'string | number'.
15+
// Property 'length' does not exist on type 'number'.
16+
```
17+
18+
而有时候,我们确实需要在还不确定类型的时候就访问其中一个类型的属性或方法,比如:
19+
20+
```ts
21+
function getLength(something: string | number): number {
22+
if (something.length) {
23+
return something.length;
24+
} else {
25+
return something.toString().length;
26+
}
27+
}
28+
29+
// index.ts(2,17): error TS2339: Property 'length' does not exist on type 'string | number'.
30+
// Property 'length' does not exist on type 'number'.
31+
// index.ts(3,22): error TS2339: Property 'length' does not exist on type 'string | number'.
32+
// Property 'length' does not exist on type 'number'.
33+
```
34+
35+
上例中,获取 `something.length` 的时候会报错。
36+
37+
此时可以使用类型断言,将 `something` 断言成 `string`
38+
39+
```ts
40+
function getLength(something: string | number): number {
41+
if ((<string>something).length) {
42+
return (<string>something).length;
43+
} else {
44+
return something.toString().length;
45+
}
46+
}
47+
```
48+
49+
类型断言的用法如上,在需要断言的变量前加上 `<Type>` 即可。
50+
51+
**类型断言不是类型转换,断言成一个联合类型中不存在的类型是不允许的**
52+
53+
```ts
54+
function toBoolean(something: string | number): boolean {
55+
return <boolean>something;
56+
}
57+
58+
// index.ts(2,10): error TS2352: Type 'string | number' cannot be converted to type 'boolean'.
59+
// Type 'number' is not comparable to type 'boolean'.
60+
```
61+
62+
## 参考
63+
64+
- [Advanced Types # Type Guards and Differentiating Types](http://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Advanced%20Types.html#类型保护与区分类型(type-guards-and-differentiating-types))
465

566
---
667

basics/type-inference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ myFavoriteNumber = 7;
2424

2525
TypeScript 会在没有明确的指定类型的时候推测出一个类型,这就是类型推论。
2626

27-
如果定义的时候没有赋值,不管之后有没有赋值,都会被推断成 `any` 类型:
27+
**如果定义的时候没有赋值,不管之后有没有赋值,都会被推断成 `any` 类型**
2828

2929
```ts
3030
let myFavoriteNumber;
@@ -34,7 +34,7 @@ myFavoriteNumber = 7;
3434

3535
## 参考
3636

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)
37+
- [Type Inference](http://www.typescriptlang.org/docs/handbook/type-inference.html) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Type%20Inference.html)
3838

3939
---
4040

basics/type-of-array.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
let fibonacci: number[] = [1, 1, 2, 3, 5];
1111
```
1212

13-
数组的项中不允许出现其他的类型
13+
数组的项中**不允许**出现其他的类型
1414

1515
```ts
1616
let fibonacci: number[] = [1, '1', 2, 3, 5];
@@ -37,7 +37,7 @@ fibonacci.push('8');
3737
let fibonacci: Array<number> = [1, 1, 2, 3, 5];
3838
```
3939

40-
关于泛型,可以参考[《泛型》](../advanced/generics.md)一章。
40+
关于泛型,可以参考[泛型](../advanced/generics.md)一章。
4141

4242
## 用接口表示数组
4343

@@ -81,12 +81,12 @@ function sum() {
8181
}
8282
```
8383

84-
关于内建类型,可以参考[内置对象](./built-in-objects.md)一章。
84+
关于内置对象,可以参考[内置对象](./built-in-objects.md)一章。
8585

8686
## Links
8787

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#数组类型)
88+
- [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+
- [Interfaces # Indexable Types](http://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types) | [中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Interfaces.html#数组类型)
9090

9191
---
9292

0 commit comments

Comments
 (0)