Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd94b9a

Browse files
authored
Merge pull request xcatliu#7 from jokester/minor-fix
补充一些enum和类型的内容
2 parents 5c1566c + 61806a3 commit bd94b9a

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

advanced/enum.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,26 @@ var Days;
8484

8585
所以使用的时候需要注意,最好不要出现这种覆盖的情况。
8686

87-
手动赋值的枚举项只能为数字,任何其他类型都是不被允许的
87+
手动赋值的枚举项可以不是数字,此时需要使用类型断言来让tsc无视类型检查 (编译出的js仍然是可用的)
8888

8989
```ts
90-
enum Days {Sun = 7, Mon, Tue, Wed, Thu, Fri, Sat = "S"};
90+
enum Days {Sun = 7, Mon, Tue, Wed, Thu, Fri, Sat = <any>"S"};
91+
```
9192

92-
// index.ts(1,52): error TS2322: Type 'string' is not assignable to type 'Days'.
93+
```js
94+
var Days;
95+
(function (Days) {
96+
Days[Days["Sun"] = 7] = "Sun";
97+
Days[Days["Mon"] = 8] = "Mon";
98+
Days[Days["Tue"] = 9] = "Tue";
99+
Days[Days["Wed"] = 10] = "Wed";
100+
Days[Days["Thu"] = 11] = "Thu";
101+
Days[Days["Fri"] = 12] = "Fri";
102+
Days[Days["Sat"] = "S"] = "Sat";
103+
})(Days || (Days = {}));
93104
```
94105

95-
当然,手动赋值的枚举项也可以为小数或负数,此时后续未手动赋值的项的递增步长任为 `1`
106+
当然,手动赋值的枚举项也可以为小数或负数,此时后续未手动赋值的项的递增步长仍为 `1`
96107

97108
```ts
98109
enum Days {Sun = 7, Mon = 1.5, Tue, Wed, Thu, Fri, Sat};

basics/type-assertion.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
# 类型断言
22

3-
类型断言(Type Assertion)用来手动将一个联合类型的变量指定为一个更加具体的类型
3+
类型断言(Type Assertion)可以用来绕过编译器的类型推断,手动指定一个值的类型(即程序员对编译器**断言**
44

5-
## 简单的例子
5+
## 语法
6+
7+
```ts
8+
<类型>
9+
10+
//
11+
12+
as 类型
13+
14+
// 在TSX语法 (React的JSX语法的TS版)中必须用后一种
15+
```
16+
17+
## 例子:将一个联合类型的变量指定为一个更加具体的类型
618

719
[之前提到过](union-types.md#访问联合类型的属性或方法),当 TypeScript 不确定一个联合类型的变量到底是哪个类型的时候,我们**只能访问此联合类型的所有类型里共有的属性或方法**
820

@@ -61,6 +73,7 @@ function toBoolean(something: string | number): boolean {
6173

6274
## 参考
6375

76+
- [TypeScript Deep Dive / Type Assertion](https://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html)
6477
- [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))
6578

6679
---

basics/type-inference.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@ myFavoriteNumber = 7;
2424

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

27-
**如果定义的时候没有赋值,不管之后有没有赋值,都会被推断成 `any` 类型**
27+
**在TypeScript 2.1 之前,如果定义的时候没有赋值,不管之后有没有赋值,都会被推断成 `any` 类型而完全不被类型检查**
2828

2929
```ts
3030
let myFavoriteNumber;
3131
myFavoriteNumber = 'seven';
3232
myFavoriteNumber = 7;
3333
```
3434

35+
**TypeScript 2.1 中,编译器会考虑对 myFavoriteNumber 的最后一次赋值来检查类型**[](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#improved-any-inference)
36+
3537
## 参考
3638

3739
- [Type Inference](http://www.typescriptlang.org/docs/handbook/type-inference.html)[中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Type%20Inference.html)
40+
- [TypeScript 2.1 Release Note](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html)
3841

3942
---
4043

0 commit comments

Comments
 (0)