TypeScript 2.5

可选的 catch 条款变量

🌐 Optional catch clause variables

多亏了 @tinganho 的工作,TypeScript 2.5 实现了一个新的 ECMAScript 特性,允许用户在 catch 子句中省略变量。例如,在使用 JSON.parse 时,你可能需要用 try/catch 来封装对函数的调用,但你可能最终并不会使用在输入错误时抛出的 SyntaxError

🌐 Thanks to work done by @tinganho, TypeScript 2.5 implements a new ECMAScript feature that allows users to omit the variable in catch clauses. For example, when using JSON.parse you may need to wrap calls to the function with a try/catch, but you may not end up using the SyntaxError that gets thrown when input is erroneous.

ts
let input = "...";
try {
JSON.parse(input);
} catch {
// ^ Notice that our `catch` clause doesn't declare a variable.
console.log("Invalid JSON given\n\n" + input);
}

checkJs/@ts-check 模式下的类型断言/类型转换语法

🌐 Type assertion/cast syntax in checkJs/@ts-check mode

TypeScript 2.5 引入了在项目中使用普通 JavaScript 时断言表达式的类型的功能。语法是一个 /** @type {...} */ 注释标注,后跟一个需要重新评估类型的括号表达式。例如:

🌐 TypeScript 2.5 introduces the ability to assert the type of expressions when using plain JavaScript in your projects. The syntax is an /** @type {...} */ annotation comment followed by a parenthesized expression whose type needs to be re-evaluated. For example:

ts
var x = /** @type {SomeType} */ AnyParenthesizedExpression;

去重和重定向的包

🌐 Deduplicated and redirected packages

在 TypeScript 2.5 中使用 Node 模块解析策略进行导入时,编译器现在会检查文件是否来自“相同”的包。 如果文件来自一个包含与之前遇到的包相同的 nameversion 字段的 package.json 包,那么 TypeScript 会将其重定向到最顶层的包。 这有助于解决两个包可能包含相同类声明的问题,但其中包含 private 成员导致它们在结构上不兼容。

🌐 When importing using the Node module resolution strategy in TypeScript 2.5, the compiler will now check whether files originate from “identical” packages. If a file originates from a package with a package.json containing the same name and version fields as a previously encountered package, then TypeScript will redirect itself to the top-most package. This helps resolve problems where two packages might contain identical declarations of classes, but which contain private members that cause them to be structurally incompatible.

作为一个额外的好处,这也可以通过避免从重复的包中加载 .d.ts 文件来减少编译器和语言服务的内存和运行时占用。

🌐 As a nice bonus, this can also reduce the memory and runtime footprint of the compiler and language service by avoiding loading .d.ts files from duplicate packages.

🌐 The --preserveSymlinks compiler flag

TypeScript 2.5 引入了 preserveSymlinks 标志,它的行为与 Node.js 中的 --preserve-symlinks 标志 类似。这个标志的行为也与 Webpack 的 resolve.symlinks 选项相反(即将 TypeScript 的 preserveSymlinks 设置为 true 与将 Webpack 的 resolve.symlinks 设置为 false 相对应,反之亦然)。

🌐 TypeScript 2.5 brings the preserveSymlinks flag, which parallels the behavior of the --preserve-symlinks flag in Node.js. This flag also exhibits the opposite behavior to Webpack’s resolve.symlinks option (i.e. setting TypeScript’s preserveSymlinks to true parallels setting Webpack’s resolve.symlinks to false, and vice-versa).

在此模式下,对模块和包的引用(例如 import/// <reference type="..." /> 指令)都是相对于符号链接文件的位置进行解析,而不是相对于符号链接解析到的路径进行解析。

为了一个更具体的例子,我们将参考 Node.js 网站上的文档

🌐 In this mode, references to modules and packages (e.g. imports and /// <reference type="..." /> directives) are all resolved relative to the location of the symbolic link file, rather than relative to the path that the symbolic link resolves to. For a more concrete example, we’ll defer to the documentation on the Node.js website.