TypeScript 2.0 正式版已经发布了:〔译〕TypeScript 2.0 正式版发布
不过可能你们更关心的是 〔译〕TypeScript 2.0 的新特性html
原文:Announcing TypeScript 2.0 RC
August 30, 2016 by Daniel Rosenwassernode
TypeScript 2.0 候选发行版(RC)出来了,离 TypeScript 2.0 最终发布也就不远了,赞!若是你还没开始使用 TypeScript,能够先看看网站上的教程。git
要使用 RC 版本,能够下载 TypeScript 2.0 RC for Visual Studio 2015(须要 VS2015 Update 3);也能够经过 NuGet 下载,或者像下面这样使用 npm:程序员
npm install -g typescript@rc
Visual Studio Code 用户想使用 RC 版本请参考这里。github
这个 RC 版本让你们看到 2.0 正式版的样子,咱们经过这个版本普遍收集用户反馈,将 2.0 打造得更加稳定可靠。总的来讲,通常状况下 RC 版本已经足够稳定了,而且咱们不但愿再往上加新的特性。typescript
不过,自 2.0 Beta 发布以来,已经加了很多东西,因此下面可能会有你还没有据说的新特性。npm
译者注json
对于 Tagged Unions 的翻译,我查了不少资料,在 wiki 上找到以下描述:a tagged union, also called a variant, variant record, discriminated union, disjoint union, or sum type。其中 Variant 这个说法在 VB 中十分经常使用。在参考了 C# 对
var
关键字的翻译以后,我决定将其翻译为“推断类型”。segmentfault推断类型是一种数据结构,很像联合(C/C++程序员必定知道这个结构)。它有一个字段(或称为属性)用于识别当前结构的确切类型。(参考 What is a tagged union)安全
推断类型使 JavaScript 在某些方向更像 F#、Swift 等语言。为此,JavaScript 程序员们必定会很是高兴。这个特性也叫 可识别联合、互斥联合 或 代理类型。不过特性自己显然比名称更有意思。
假设有两个类型:Circle
和 Square
,而后定义它们的联合类型,命名为 Shape
。
interface Circle { kind: "circle"; radius: number; } interface Square { kind: "square"; sideLength: number; } type Shape = Circle | Square;
注意 Circle
和 Square
都有一个叫 kind
的字段,保存的字符串常数,表示类型。也就是说 Circle
的 kind
老是 "circle"
。每一个类型都有一个共同的字段,但经过不一样的值做为 标记 区分开来。
在 TypeScript 1.8 中,若是写一个获取面积的函数,须要判断 Shape
的每种类型。
function getArea(shape: Shape) { switch (shape.kind) { case "circle": // 从 'Shape' 转换为 'Circle' let c = shape as Circle; return Math.PI * c.radius ** 2; case "square": // 从 'Shape' 转换为 'Square' let sq = shape as Square; return sq.sideLength ** 2; } }
注意到咱们为每种图形都使用了一个中间变量来使代码看起来简洁。
在 2.0 中就再也不须要中间变量了。语言懂得如何经过 kind
来辨别类型,因此你能够少写点代码
function getArea(shape: Shape) { switch (shape.kind) { case "circle": // 这里 'shape' 是 'Circle' return Math.PI * shape.radius ** 2; case "square": // 这里 'shape' 是 'Square' return shape.sideLength ** 2; } }
上面的代码彻底正确,TypeScript 能经过流程控制分析每一个分支上的正确类型。可使用 --noImplicitReturns
和即将可用的 --strictNullChecks
特性保证这些检查更完全。
推断类型让 JavaScript 这种形式下的代码更简洁也更安全。例如,像 Redux 这样的库常用这种形式的来处理 action。每一个独立的
1.8 带来的字符串字面类型很是有用,就像上面看到的那样,能够用它来处理推断类型。
除了字符串,咱们还想提供更多的类型。在 2.0 中,每一个独特的布尔、数值或枚举成员均可以拥有本身的类型!
type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; let nums: Digit[] = [1, 2, 4, 8]; // 错误! '16' 不是 'Digit'! nums.push(16);
这样在使用推断类型时,咱们能够快速而地处理一些事情而毫无违和感。
interface Success<T> { success: true; value: T; } interface Failure { success: false; reason: string; } type Result<T> = Success<T> | Failure;
这里的 Result<T>
类型可能表示失败。若是表示成功,它有一个值,若是表示失败,它包含表示失败缘由的 reson
字段。value
字段仅在 success
是 true
的时候有效。
declare function tryGetNumUsers(): Result<number>; let result = tryGetNumUsers(); if (result.success === true) { // 'result' 是 'Success<number>' 类型的 console.log(`Server reported ${result.value} users`); } else { // 'result'是 'Failure' 类型的 console.error("Error fetching number of users!", result.reason); }
你可能已经注意到了,枚举值也能够拥有它们本身的类型!
enum ActionType { Append, Erase } interface AppendAction { type: ActionType.Append; text: string; } interface EraseAction { type: ActionType.Erase; numChars: number; } function updateText(currentText: string, action: AppendAction | EraseAction) { if (action.type === ActionType.Append) { // 'action' has type 'AppendAction' return currentText + action.text; } else { // 'action' has type 'EraseAction' return currentText.slice(0, -action.numChars); } }
译者注
Globs 直译是“团块”的意思,不过这显然不如
Globs
自己意思明确。因此这里我没有翻译这个词。关于 Globs,能够参考 node-blog 在 README.md 中的说明。
首次向你们介绍 tsconfig.json 文件的时候,手工列出全部文件实在痛苦。TypeScript 1.6 引入了 excludes
配置来缓解这个问题;然而,这显然不够。痛苦在于,写完了每条文件路径,仍然会有问题发生,结果是由于忘了排除新文件。
TypeScript 2.0 终于开始支持 Globs 语法。Globs 容许咱们在路径中使用通配符,这样一来,写路径不再是件乏味的事了。
include
和 exclude
配置中均可以使用 Globs 语法。来看一个 tsconfig.json 的示例:
{ "include": [ "./src/**/*.ts" ], "exclude": [ "./src/tests/**" ] }
TypeScript globs 语法支持以下通配符:
*
匹配 0 个或多个字符,分隔符(好比 /
或 \
)除外?
精确匹配 1 个字符,分隔符除外**/
匹配任意层子目录以前提到,TypeScript 2.0 很快就发布了,可是使用 RC 版本带来的 2.0 的新特性会为社区发展带来巨大的做用。
若是发现任何问题,能够 经过Github 反馈给咱们。咱们很是愿意听到你尝试以后给咱们的反馈。祝愉快!