⚠️ ARNING⚠️:本文中的所有内容均使用 Node.js 实验性功能。请确保你使用的 Node.js 版本支持本文中提到的功能。请记住,实验性功能可能会在 Node.js 的未来版本中发生变化。
¥⚠️WARNING⚠️: All content in this article uses Node.js experimental features. Please make sure you are using a version of Node.js that supports the features mentioned in this article. And remember that experimental features can change in future versions of Node.js.
原生运行 TypeScript
¥Running TypeScript Natively
在上一篇文章中,我们学习了如何使用转译和运行器运行 TypeScript 代码。在本文中,我们将学习如何使用 Node.js 本身运行 TypeScript 代码。
¥In the previous articles, we learned how to run TypeScript code using transpilation and with a runner. In this article, we will learn how to run TypeScript code using Node.js itself.
使用 Node.js 运行 TypeScript 代码
¥Running TypeScript code with Node.js
自 V22.6.0 以来,Node.js 通过 "类型剥离" 为某些 TypeScript 语法提供了实验性支持。你可以直接在 Node.js 中编写有效的 TypeScript 代码,而无需先对其进行转译。
¥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.
--experimental-strip-types
标志告诉 Node.js 在运行 TypeScript 代码之前从中剥离类型注释。
¥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
就是这样!你现在可以在 Node.js 中直接运行 TypeScript 代码,而无需先对其进行转译,并使用 TypeScript 捕获与类型相关的错误。
¥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.
在 V22.7.0 中,此实验性支持已扩展为转换仅限 TypeScript 的语法,如 enum
和 namespace
,并添加了 --experimental-transform-types
标志。启用 --experimental-transform-types
自动意味着启用 --experimental-strip-types
,因此无需在同一个命令中使用两个标志:
¥In V22.7.0 this experimental support was extended to transform TypeScript-only syntax, like enum
s 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
从 V23 开始,--experimental-strip-types
标志默认启用(你可以通过 --no-experimental-strip-types
标志禁用它),使你能够运行任何受支持的语法,因此支持使用 node file.ts
运行以下文件:
¥From V23 onwards, the --experimental-strip-types
flag is enabled by default (you can disable it via the --no-experimental-strip-types
flag), 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';
}
但是,运行任何需要转换的代码,如下面的代码仍然需要使用 --experimental-transform-types
:
¥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);
Node.js 的未来版本将包含对 TypeScript 的支持,而无需命令行标志。
¥Future versions of Node.js will include support for TypeScript without the need for a command line flag.
限制
¥Limitations
在撰写本文时,Node.js 中对 TypeScript 的实验性支持存在一些限制。
¥At the time of writing, the experimental support for TypeScript in Node.js has some limitations.
你可以获取有关 API 文档 的更多信息。
¥You can get more information on the API docs.
配置
¥Configuration
Node.js TypeScript 加载器(Amaro)不需要或使用 tsconfig.json
来运行 TypeScript 代码。
¥The Node.js TypeScript loader (Amaro) does not need or use tsconfig.json
to run TypeScript code.
我们建议配置你的编辑器和 tsc
以反映 Node.js 行为,方法是使用 compilerOptions
列出的 此处 创建 tsconfig.json
,以及使用 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.
重要说明
¥Important notes
感谢所有使此功能成为可能的贡献者。我们希望此功能很快会在 Node.js 的 LTS 版本中稳定可用。
¥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.