在 Node.js 中原生运行 TypeScript
Running TypeScript Natively in Node.js

原始链接: https://nodejs.org/en/learn/typescript/run-natively

自 Node.js 22.6.0 版本起,开始逐步支持直接运行 TypeScript 代码,无需预先编译。这通过“类型剥离”实现,最初通过 `--experimental-strip-types` 标志启用。22.7.0 版本增加了 `--experimental-transform-types` 来处理 TypeScript 特有的语法,例如枚举,同时也自动启用了类型剥离。 从 Node.js v23.6.0 版本开始,类型剥离**默认启用**,允许使用 `node file.ts` 直接执行基本的 TypeScript 代码。然而,需要转换的代码仍然需要 `--experimental-transform-types`。 值得注意的是,此功能不依赖于 `tsconfig.json`,但建议将您的编辑器/TSC 配置与 Node.js 的行为保持一致(使用 TypeScript 5.7+)。虽然目前仍处于实验阶段并存在限制,但目标是在未来的版本中实现完全的、无需标志的 TypeScript 支持。欢迎社区提交错误报告和贡献。

## Node.js 现在支持原生 TypeScript 执行 Node.js 已经实现了对 TypeScript 的原生支持,允许其直接运行 TypeScript 代码而无需转译。这得益于内置的类型剥离器,但也引发了一个问题:为什么 Node.js 没有直接捆绑现有的工具,如 `tsc` 或 `swc`?这可能会导致在处理新的 TypeScript 特性时面临持续的维护挑战。 此次更新,以及 Node 测试框架的改进,正在帮助一些开发者减少对传统测试框架的依赖。讨论的重点在于 Node.js 最近的进步是否足以挑战 Deno 和 Bun 等较新的运行时环境,许多人认为 Node.js 庞大的生态系统仍然是其主要的优势。 虽然 Deno 和 Bun 最初的目标是与 Node 完全脱钩,但它们越来越多地关注兼容性,而 Node 则采用了它们率先推出的特性。有人建议 TypeScript 应该成为一种独立的语言,但也有人强调它作为 JavaScript 超集的作用,推动着 *JavaScript 生态系统内部* 的创新。
相关文章

原文

Since v23.6.0, Node.js enables "type stripping" by default. If you are using v23.6.0 or later and your source code contains only erasable typescript syntax, you do not need this article.

Since V22.6.0, Node.js has experimental support for some TypeScript syntax via "type stripping". You can write code that's valid TypeScript directly in Node.js without the need to transpile it first.

The --experimental-strip-types flag tells Node.js to strip the type annotations from the TypeScript code before running it.

node --experimental-strip-types example.ts

And that's it! You can now run TypeScript code directly in Node.js without the need to transpile it first, and use TypeScript to catch type-related errors.

In V22.7.0 this experimental support was extended to transform TypeScript-only syntax, like enums and namespace, with the addition of the --experimental-transform-types flag. 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

From v23.6.0 onwards, type stripping is enabled by default (you can disable it via --no-experimental-strip-types), enabling you to run any supported syntax, so running files like the one below with node file.ts is supported:

function foo(bar: number): string {
  return 'hello';
}

However, running any code that requires transformations, like the code below still needs the use of --experimental-transform-types:

enum MyEnum {
  A,
  B,
}

console.log(MyEnum.A);

Future versions of Node.js will include support for TypeScript without the need for a command line flag.

At the time of writing, the experimental support for TypeScript in Node.js has some limitations.

You can get more information on the API docs.

Configuration

The Node.js TypeScript loader (Amaro) does not need or use tsconfig.json to run TypeScript code.

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.

Thanks to all the contributors who have made this feature possible. We hope that this feature will be stable and available in the LTS version of Node.js soon.

We can understand that this feature is experimental and has some limitations; if that doesn't suit your use-case, please use something else, or contribute a fix. Bug reports are also welcome, please keep in mind the project is run by volunteers, without warranty of any kind, so please be patient if you can't contribute the fix yourself.

联系我们 contact @ memedata.com