TypeScript 功能


¥TypeScript features

由于 Node.js 仅删除内联类型,因此任何涉及用新 JavaScript 语法替换 TypeScript 语法的 TypeScript 功能都会出错,除非传递了标志 --experimental-transform-types

¥Since Node.js is only removing inline types, any TypeScript features that involve replacing TypeScript syntax with new JavaScript syntax will error, unless the flag --experimental-transform-types is passed.

需要转换的最突出的功能是:

¥The most prominent features that require transformation are:

  • Enum 声明

    ¥Enum declarations

  • 带有运行时代码的 namespace

    ¥namespace with runtime code

  • 带有运行时代码的旧 module

    ¥legacy module with runtime code

  • 参数属性

    ¥parameter properties

  • 导入别名

    ¥import aliases

支持不包含运行时代码的 namespacesmodule。此示例将正常工作:

¥namespaces and module that do not contain runtime code are supported. This example will work correctly:

// This namespace is exporting a type
namespace TypeOnly {
   export type A = string;
} 

这将导致 ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX 错误:

¥This will result in ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX error:

// This namespace is exporting a value
namespace A {
   export let x = 1
} 

由于装饰器当前是 TC39 第 3 阶段提案,并且很快将得到 JavaScript 引擎的支持,因此它们不会被转换,并将导致解析器错误。这是一个暂时的限制,将来会得到解决。

¥Since Decorators are currently a TC39 Stage 3 proposal and will soon be supported by the JavaScript engine, they are not transformed and will result in a parser error. This is a temporary limitation and will be resolved in the future.

此外,Node.js 不读取 tsconfig.json 文件,也不支持依赖于 tsconfig.json 中的设置的功能,例如路径或将较新的 JavaScript 语法转换为较旧的标准。

¥In addition, Node.js does not read tsconfig.json files and does not support features that depend on settings within tsconfig.json, such as paths or converting newer JavaScript syntax into older standards.