From 6f3f1d1918b6ff9a9f27b8c5092d04e7e2d6f806 Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Sun, 29 Jan 2017 17:21:59 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=96=AD=E8=A8=80?= =?UTF-8?q?=E7=9A=84=E4=B8=A4=E7=A7=8D=E8=AF=AD=E6=B3=95=E5=92=8C=E7=94=A8?= =?UTF-8?q?=E9=80=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- basics/type-assertion.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/basics/type-assertion.md b/basics/type-assertion.md index 7a02f5f9..baea93c1 100644 --- a/basics/type-assertion.md +++ b/basics/type-assertion.md @@ -1,8 +1,20 @@ # 类型断言 -类型断言(Type Assertion)用来手动将一个联合类型的变量指定为一个更加具体的类型。 +类型断言(Type Assertion)可以用来绕过编译器的类型推断,手动指定一个值的类型(即程序员对编译器**断言**)。 -## 简单的例子 +## 语法 + +```ts +<类型>值 + +// 或 + +值 as 类型 + +// 在TSX语法 (React的JSX语法的TS版)中必须用后一种 +``` + +## 例子:将一个联合类型的变量指定为一个更加具体的类型 [之前提到过](union-types.md#访问联合类型的属性或方法),当 TypeScript 不确定一个联合类型的变量到底是哪个类型的时候,我们**只能访问此联合类型的所有类型里共有的属性或方法**: @@ -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))) --- From 30c828fc89e00627c8d6c32f577c518661f7130b Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Sun, 29 Jan 2017 16:54:05 +0900 Subject: [PATCH 2/3] =?UTF-8?q?-=E6=89=8B=E5=8A=A8=E8=B5=8B=E5=80=BC?= =?UTF-8?q?=E7=9A=84=E6=9E=9A=E4=B8=BE=E9=A1=B9=E5=8F=AA=E8=83=BD=E4=B8=BA?= =?UTF-8?q?=E6=95=B0=E5=AD=97=20+=E6=89=8B=E5=8A=A8=E8=B5=8B=E5=80=BC?= =?UTF-8?q?=E7=9A=84=E6=9E=9A=E4=B8=BE=E9=A1=B9=E5=8F=AF=E4=BB=A5=E4=B8=8D?= =?UTF-8?q?=E6=98=AF=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- advanced/enum.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/advanced/enum.md b/advanced/enum.md index 4d2ee7af..9f37a27e 100644 --- a/advanced/enum.md +++ b/advanced/enum.md @@ -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 = "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}; From 61806a3503a75dcab7e4d11f4b0d9318184ecaf2 Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Sun, 29 Jan 2017 17:06:30 +0900 Subject: [PATCH 3/3] =?UTF-8?q?TS2.1=E4=B8=AD=E5=A2=9E=E5=BC=BA=E4=BA=86?= =?UTF-8?q?=E5=AF=B9any=E7=9A=84=E7=B1=BB=E5=9E=8B=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- basics/type-inference.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/basics/type-inference.md b/basics/type-inference.md index 8f3809c3..51752e4f 100644 --- a/basics/type-inference.md +++ b/basics/type-inference.md @@ -24,7 +24,7 @@ myFavoriteNumber = 7; TypeScript 会在没有明确的指定类型的时候推测出一个类型,这就是类型推论。 -**如果定义的时候没有赋值,不管之后有没有赋值,都会被推断成 `any` 类型**: +**在TypeScript 2.1 之前,如果定义的时候没有赋值,不管之后有没有赋值,都会被推断成 `any` 类型而完全不被类型检查**: ```ts let myFavoriteNumber; @@ -32,9 +32,12 @@ 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) ---