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 阶段提案,它们不会被转换,因此会导致解析器错误。Node.js 不提供 polyfill,因此在 JavaScript 原生支持装饰器之前,Node.js 不会支持装饰器。

¥Since Decorators are currently a TC39 Stage 3 proposal, they are not transformed and will result in a parser error. Node.js does not provide polyfills and thus will not support decorators until they are supported natively in JavaScript.

此外,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.