可选 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
模块解析策略导入时,编译器现在将检查文件是否源自 “identical” 包。如果文件来自一个包,该包的 package.json
包含与之前遇到的包相同的 name
和 version
字段,则 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.
--preserveSymlinks
编译器标志
¥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. import
s 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.