类型剥离
¥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.