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

Skip to content

补充一些enum和类型的内容 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions advanced/enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,26 @@ var Days;

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

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

```ts
enum Days {Sun = 7, Mon, Tue, Wed, Thu, Fri, Sat = "S"};
enum Days {Sun = 7, Mon, Tue, Wed, Thu, Fri, Sat = <any>"S"};
```

// index.ts(1,52): error TS2322: Type 'string' is not assignable to type 'Days'.
```js
var Days;
(function (Days) {
Days[Days["Sun"] = 7] = "Sun";
Days[Days["Mon"] = 8] = "Mon";
Days[Days["Tue"] = 9] = "Tue";
Days[Days["Wed"] = 10] = "Wed";
Days[Days["Thu"] = 11] = "Thu";
Days[Days["Fri"] = 12] = "Fri";
Days[Days["Sat"] = "S"] = "Sat";
})(Days || (Days = {}));
```

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

```ts
enum Days {Sun = 7, Mon = 1.5, Tue, Wed, Thu, Fri, Sat};
Expand Down
17 changes: 15 additions & 2 deletions basics/type-assertion.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
# 类型断言

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

## 简单的例子
## 语法

```ts
<类型>值

// 或

值 as 类型

// 在TSX语法 (React的JSX语法的TS版)中必须用后一种
```

## 例子:将一个联合类型的变量指定为一个更加具体的类型

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

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

## 参考

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

---
Expand Down
5 changes: 4 additions & 1 deletion basics/type-inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ myFavoriteNumber = 7;

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

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

```ts
let myFavoriteNumber;
myFavoriteNumber = 'seven';
myFavoriteNumber = 7;
```

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

## 参考

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

---

Expand Down