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 声明
  • namespace 与运行时代码
  • 带有运行时代码的传统 module
  • 参数属性
  • 导入别名

不包含运行时代码的 namespacesmodule 是被支持的。这个示例将会正确运行:

// 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.