Node.js 现在默认支持 TypeScript
Refer
Source
: Node.js Now Supports TypeScript By Default
Author
: Matt Pocock
Translator
: SenaoXi
Release Time
: January 14, 2025
Copyright Statement
Translation and Republication Notice:
This translation is provided for educational and informational purposes only. All intellectual property rights, including copyright, remain with the original author and/or publisher. This translation maintains the integrity of the original content while making it accessible to chinese readers.
Modifications Disclosure:
- This is a complete and faithful translation of the original content with no substantive modifications.
- This translation includes minor adaptations to improve clarity for chinese readers while preserving all essential information and viewpoints.
- Sections marked with [†] contain supplementary explanations added by the translator to provide cultural or technical context.
Rights Reservation:
If you are the copyright holder and believe this translation exceeds fair use guidelines, please contact us at email. We are committed to respecting intellectual property rights and will promptly address any legitimate concerns.
核心要点
Node 23
即将能够无需任何额外配置直接运行 TypeScript
文件。
Marco Ippolito
(过去一年一直推动 Node
中的 TypeScript
支持)在 Node 23
中提交了一个 PR,取消了对 --experimental-strip-types
的实验性标记。
实际上,这意味着:
- 你可以创建包含
TS
语法(如类型注解)的index.ts
文件。 - 你可以直接运行
node index.ts
,无需任何额外参数。 Node
将使用swc
的一个版本来剥离类型,然后运行生成的代码。
常见问题解答
如何尝试这个功能?
该功能现已在 Node Nightly
版本中可用,正如 Marco
所提到的。几天后它很可能会在 Node 23
中发布。更新:看起来它将在 23.6.0 版本中推出。
Node 会对我的文件进行类型检查吗?
不会。Node
在运行文件时不会进行类型检查。这是一件好事 - 这意味着类型检查可以在与应用程序运行分离的进程中进行。你需要在本地运行 tsc --watch
来在工作时检查应用程序的类型。
tsconfig.json
中应该包含什么?
这是我的 TSConfig 速查表 的定制版本:
{
"compilerOptions": {
/* Base Options: */
"esModuleInterop": true,
"skipLibCheck": true,
"target": "es2022",
"allowJs": true,
"resolveJsonModule": true,
"moduleDetection": "force",
"isolatedModules": true,
"verbatimModuleSyntax": true,
/* Strictness */
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
/* Node Stuff */
"allowImportingTsExtensions": true,
"rewriteRelativeImportExtensions": true,
"module": "NodeNext",
"noEmit": true,
/* If your code doesn't run in the DOM: */
"lib": ["es2022"]
}
}
这在 TS 5.8
中可能会发生变化,TypeScript
团队计划发布一个兼容性标志。目前尚不确定具体内容,但它将使与 Node
的 TS
支持的互操作性更容易。
支持枚举和命名空间吗?
默认情况下不支持枚举和命名空间。查看 此处 了解其他不支持的功能。如果你想支持其他 TS
功能,可以添加 --experimental-transform-types
使其工作。
我对此并不太难过,尤其是关于 枚举 这部分。
注意,不支持运行时命名空间,但支持仅类型命名空间(使用 declare namespace
)。这很好 - 我喜欢仅类型命名空间。
生产环境中需要将代码转译为 JavaScript 吗?
Node 应用
如果你将代码部署到无服务器平台,是的。转译和压缩代码对于获得良好的冷启动至关重要。
如果你部署到冷启动影响较小的地方,那么可能不需要!在应用程序最初加载 TypeScript
时会有一点小成本(确实很小),但一旦进程运行,不太可能导致速度减慢。
一个可能的问题是,如果你的代码在处理过程中动态加载其他 TypeScript
文件。这可能会导致一些额外的速度减慢 - 所以谨慎行事。
库
是的,在将库发布到 NPM
之前,你仍然需要将 TypeScript
转译为 JavaScript
。原因有两个:
- 让
JavaScript
用户仍然可以使用你的库。 TypeScript
在使用声明文件(.d.ts
文件)时运行更快,所以不提供这些文件意味着你使消费者的TS
体验变慢。
Monorepos
如果你在 monorepo
中创建一个库,仅供该 monorepo
使用,我仍然建议将 TypeScript
文件转译为 JavaScript
。
原因与库相同 - monorepo
中的 .d.ts
文件越多,TS
体验就越快。
这个功能会来到之前的 Node 版本吗?
是的,TypeScript
支持将会添加到 Node 22
,但不会添加到 Node 20
。Marco
在 X
平台上 提到了这一点。