本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
AL2023 中的 TypeScript
注意
本文档提供了关于 TypeScript 及其基于 Node.js 的执行环境的基本信息。它还涵盖了典型的开发工作流程,并解释了 TypeScript 如何打包在 AL2023 中以提供一致且可复现的开发环境。
TypeScriptnodejs20-typescript 或 nodejs22-typescript)安装 TS 编译器的方式:在系统级别全局安装,并为每个受支持的 Node.js 版本单独安装。
tsc 不直接依赖于任何 Node.js 版本。编译器期望一定级别的运行时功能,这些功能通过选项(如 target
基于 Node.js 的运行时设计存在一个弱点:它在一台主机上仅支持一个运行时版本,并且要求在项目级别所有依赖项具有可复现性和一致性。这导致了以下使用 TypeScript 的常见方法:TS 编译器、当前 Node.js 版本的 TS 基础配置以及所有软件依赖项都在项目内部本地安装。虽然全局安装的 node 模块预期仅为 CLI 工具,例如 npm,但同样作为 CLI 工具的 tsc 却很少被全局安装。值得庆幸的是,tsc 的全局(系统范围内)和本地(项目内)安装可以毫无问题地共存,并且可以是独立使用的不同版本。请注意,本地安装的 tsc 应使用与 npm 一起安装的 npx 工具来执行。因此,即使有系统 TS 编译器,用户也有机会选择运行时组件的版本,例如 Node.js(通过 alternatives 切换活动版本)、TS 编译器(通过本地安装,或者全局安装并通过 alternatives 切换活动版本),并根据特定需求对其进行配置。
Amazon Linux 打包 TS 编译器的方式与其他全局安装的 node 模块(例如 npm)相同,基于每个 Node.js 版本进行。程序包和二进制文件均采用命名空间组织方式,并且在其名称中包含 Node.js 的主版本号。编译器的默认可执行文件名称 tsc 在运行时由 alternatives 工具管理,并指向当前活动的、为其安装并将由其执行的 Node.js 版本。此选择不依赖于当前的 Node.js 运行时版本。可能会出现 node 可执行文件指向 Node.js 20,而 tsc 被配置为由 Node.js 22 解释的情况。也可以使用 TS 编译器的命名空间名称,例如 tsc-{MAJOR_VERSION},这与默认的 tsc 名称配置为何无关。
用于管理 TS 编译器活动版本的一些有用命令
-
检查 alternatives 的配置内容
alternatives --list -
检查 tsc 的当前配置
alternatives --display tsc -
交互式更改 tsc 版本
alternatives --config tsc -
切换到手动模式并选择特定版本
alternatives --set tsc /usr/bin/tsc-{MAJOR_VERSION} -
切换回自动版本选择模式
alternatives --auto tsc
在同一系统上安装和使用多个 Node 版本及 TS 编译器的示例:
# Check the AL2023 release $ cat /etc/amazon-linux-release Amazon Linux release 2023.9.20250929 (Amazon Linux) # Install a TypeScript compiler for Node.js 20 and 22 # Node.js 20 and 22 will be installed automatically $ sudo dnf install -qy nodejs20-typescript nodejs22-typescript # Check what was installed $ rpm -q nodejs20 nodejs20-typescript nodejs22 nodejs22-typescript nodejs20-20.19.5-1.amzn2023.0.1.x86_64 nodejs20-typescript-5.9.2-1.amzn2023.0.1.noarch nodejs22-22.19.0-1.amzn2023.0.1.x86_64 nodejs22-typescript-5.9.2-1.amzn2023.0.1.noarch # Check the active version of Node.js - it is version 20 $ alternatives --display node node - status is auto. link currently points to /usr/bin/node-20 /usr/bin/node-20 - priority 100 slave npmrc: /usr/lib/nodejs20/lib/node_modules/npm/npmrc slave npm: /usr/bin/npm-20 slave npx: /usr/bin/npx-20 slave node_modules: /usr/lib/nodejs20/lib/node_modules /usr/bin/node-22 - priority 100 slave npmrc: /usr/lib/nodejs22/lib/node_modules/npm/npmrc slave npm: /usr/bin/npm-22 slave npx: /usr/bin/npx-22 slave node_modules: /usr/lib/nodejs22/lib/node_modules Current 'best' version is /usr/bin/node-20. # Check the active JS runtime version for TypeScript # Currently, the tsc compiler will be executed by Node.js 22 $ alternatives --display tsc tsc - status is auto. link currently points to /usr/bin/tsc-22 /usr/bin/tsc-22 - priority 100 slave tsserver: /usr/bin/tsserver-22 /usr/bin/tsc-20 - priority 100 slave tsserver: /usr/bin/tsserver-20 Current 'best' version is /usr/bin/tsc-22. # Check versions printed by executables $ node -v v20.19.5 $ tsc -v Version 5.9.2 # while the node is 20, tsc is executed by node 22 anyway $ head -1 /usr/bin/tsc #!/usr/bin/node-22 # However, instead of default executable names, e.g. node or tsc, # we can use namespaced names to target any installed version $ node-20 -v v20.19.5 $ node-22 -v v22.19.0 $ tsc-20 -v Version 5.9.2 $ tsc-22 -v Version 5.9.2 $ head -1 /usr/bin/tsc-20 #!/usr/bin/node-20 $ head -1 /usr/bin/tsc-22 #!/usr/bin/node-22