🌐 Running TypeScript Natively
你可以直接在 Node.js 中编写有效的 TypeScript 代码,而无需先对其进行转译。
🌐 You can write code that's valid TypeScript directly in Node.js without the need to transpile it first.
Node.js 通过一种称为类型剥离的轻量级过程运行 TypeScript。它移除可删除的 TypeScript 语法,例如类型注解和接口,然后运行剩下的 JavaScript。
🌐 Node.js runs TypeScript through a lightweight process called type stripping. It removes erasable TypeScript syntax, such as type annotations and interfaces, then runs the remaining JavaScript.
如果你使用的是 v22.18.0 或更高版本,并且你的源代码仅包含可擦除的 TypeScript 语法,你可以在不使用任何标志的情况下执行 TypeScript 代码。
🌐 If you are using v22.18.0 or later and your source code contains only erasable TypeScript syntax, you can execute TypeScript code without any flags.
node example.ts如果你使用的版本低于 v22.18.0,可以使用 --experimental-strip-types 标志直接在 Node.js 中运行 TypeScript 文件。
🌐 If you are using a version less than v22.18.0, you can use the --experimental-strip-types flag to run TypeScript files directly in Node.js.
node --experimental-strip-types example.ts就是这样!你现在可以直接在 Node.js 中运行 TypeScript 代码,而无需先进行转译。
🌐 And that's it! You can now run TypeScript code directly in Node.js without the need to transpile it first.
Node.js 在运行 TypeScript 文件时不会对你的代码进行类型检查。如果你想捕获与类型相关的错误,请单独使用 TypeScript 编译器:
🌐 Node.js does not type check your code when it runs TypeScript files. Use the TypeScript compiler separately if you want to catch type-related errors:
npx tsc --noEmit如果需要,你可以通过 --no-experimental-strip-types 标志来禁用它。
🌐 You can disable it via --no-experimental-strip-types flag if needed.
node --no-experimental-strip-types example.ts在 v22.7.0 中,添加了标志 --experimental-transform-types,用于启用仅限 TypeScript 的需要转换的语法,例如 enum 和 namespace。启用 --experimental-transform-types 会自动意味着 --experimental-strip-types 已启用,因此无需在同一命令中同时使用这两个标志:
🌐 In v22.7.0 the flag --experimental-transform-types was added to enable TypeScript-only syntax that requires transformation, like enums and namespace. Enabling --experimental-transform-types automatically implies that --experimental-strip-types is enabled, so there's no need to use both flags in the same command:
node --experimental-transform-types another-example.ts此标志是可选的,你仅应在代码需要时使用它。
🌐 This flag is opt-in, and you should only use it if your code requires it.
🌐 Constraints
Node.js 对 TypeScript 的支持有一些需要注意的限制:
🌐 The support for TypeScript in Node.js has some constraints to keep in mind:
你可以在 API 文档 上获取更多信息。
🌐 You can get more information on the API docs.
🌐 Type stripping
类型剥离只对可以在不改变运行时 JavaScript 的情况下移除的 TypeScript 语法有效。这包括常见的仅类型语法,例如类型注解、接口、类型别名以及 import type。
🌐 Type stripping only works for TypeScript syntax that can be removed without changing the runtime JavaScript. This includes common type-only syntax such as type annotations, interfaces, type aliases, and import type.
仅靠类型剥离无法处理需要 JavaScript 代码生成的语法。示例包括 enum、参数属性、带有运行时代码的命名空间以及导入别名。如果你的项目需要这些功能,请使用 --experimental-transform-types、运行器或单独的转译步骤。
🌐 Syntax that requires JavaScript code generation is not handled by type stripping alone. Examples include enum, parameter properties, namespaces with runtime code, and import aliases. Use --experimental-transform-types, a runner, or a separate transpilation step if your project needs those features.
🌐 Type checking
使用 node 运行 .ts 文件与运行 tsc 不同。Node.js 会在去除支持的类型语法后执行文件,但它不会报告类型错误。
🌐 Running a .ts file with node is not the same as running tsc. Node.js executes the file after stripping supported type syntax, but it does not report type errors.
在开发中,一个常见的设置是直接运行 Node.js 以获得快速反馈,并在单独的命令或 CI 作业中运行 tsc --noEmit 进行类型检查。
🌐 For development, a common setup is to run Node.js directly for quick feedback and run tsc --noEmit in a separate command or CI job for type checking.
🌐 Configuration
Node.js TypeScript 加载器(Amaro)在运行 TypeScript 代码时不需要也不使用 tsconfig.json。
🌐 The Node.js TypeScript loader (Amaro) does not need or use tsconfig.json to run TypeScript code.
我们建议通过创建一个使用 这里 列出的 compilerOptions 的 tsconfig.json 来配置你的编辑器和 tsc 以反映 Node.js 的行为,同时使用 TypeScript 版本 5.7 或更高。
🌐 We recommend configuring your editor and tsc to reflect Node.js behavior by creating a tsconfig.json using the compilerOptions listed here, as well as using TypeScript version 5.7 or higher.