类型剥离
¥Type stripping
¥Stability: 1.1 - Active development
标志 --experimental-strip-types
使 Node.js 能够运行 TypeScript 文件。默认情况下,Node.js 将仅执行不包含需要转换的 TypeScript 功能的文件,例如枚举或命名空间。Node.js 将用空格替换内联类型注释,并且不执行类型检查。要启用此类功能的转换,请使用标志 --experimental-transform-types
。依赖于 tsconfig.json
中的设置的 TypeScript 功能(例如路径或将较新的 JavaScript 语法转换为较旧的标准)是故意不支持的。要获得完整的 TypeScript 支持,请参阅 完全支持 TypeScript。
¥The flag --experimental-strip-types
enables Node.js to run TypeScript
files. By default Node.js will execute only files that contain no
TypeScript features that require transformation, such as enums or namespaces.
Node.js will replace inline type annotations with whitespace,
and no type checking is performed.
To enable the transformation of such features
use the flag --experimental-transform-types
.
TypeScript 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.7 或更新版本,并使用以下 tsconfig.json
设置:
¥Type stripping works with most versions of TypeScript
but we recommend version 5.7 or newer with the following tsconfig.json
settings:
{
"compilerOptions": {
"target": "esnext",
"module": "nodenext",
"allowImportingTsExtensions": true,
"rewriteRelativeImportExtensions": true,
"verbatimModuleSyntax": true
}
}