类型剥离
【Type stripping】
默认情况下,Node.js 将执行仅包含可删除 TypeScript 语法的 TypeScript 文件。Node.js 会将 TypeScript 语法替换为空格,并且不会进行类型检查。要启用不可删除的 TypeScript 语法的转换(这需要生成 JavaScript 代码),例如 enum 声明或参数属性,请使用标志 --experimental-transform-types。要禁用此功能,请使用标志 --no-experimental-strip-types。
【By default Node.js will execute TypeScript files that contains only
erasable TypeScript syntax.
Node.js will replace TypeScript syntax with whitespace,
and no type checking is performed.
To enable the transformation of non erasable TypeScript syntax, which requires JavaScript code generation,
such as enum declarations, parameter properties use the flag --experimental-transform-types.
To disable this feature, use the flag --no-experimental-strip-types.】
Node.js 会忽略 tsconfig.json 文件,因此依赖 tsconfig.json 设置的功能,例如路径或将较新的 JavaScript 语法转换为旧标准,将不被支持。要获得完整的 TypeScript 支持,请参见 完全支持 TypeScript。
【Node.js ignores tsconfig.json files and therefore
features that depend on settings within tsconfig.json,
such as paths or converting newer JavaScript syntax to older standards, are
intentionally unsupported. To get full TypeScript support, see Full TypeScript support.】
类型剥离功能旨在轻量化。通过有意不支持需要生成 JavaScript 代码的语法,并通过用空白替换内联类型,Node.js 可以运行 TypeScript 代码而无需使用源映射。
【The type stripping feature is designed to be lightweight. By intentionally not supporting syntaxes that require JavaScript code generation, and by replacing inline types with whitespace, Node.js can run TypeScript code without the need for source maps.】
类型剥离兼容大多数版本的 TypeScript,但我们建议使用 5.8 或更高版本,并使用以下 tsconfig.json 设置:
【Type stripping is compatible with most versions of TypeScript
but we recommend version 5.8 or newer with the following tsconfig.json settings:】
{
"compilerOptions": {
"noEmit": true, // Optional - see note below
"target": "esnext",
"module": "nodenext",
"rewriteRelativeImportExtensions": true,
"erasableSyntaxOnly": true,
"verbatimModuleSyntax": true
}
} 如果你打算只执行 *.ts 文件,例如构建脚本,可以使用 noEmit 选项。如果你打算分发 *.js 文件,则不需要这个标志。
【Use the noEmit option if you intend to only execute *.ts files, for example
a build script. You won't need this flag if you intend to distribute *.js
files.】