本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AL2023 中的 TypeScript
注意
本文件提供有關 TypeScript 及其 Node.js 型執行環境的基本資訊。它還涵蓋典型的開發工作流程,並說明 TypeScript 如何封裝在 AL2023 中,以提供一致且可重現的開發環境。
TypeScriptnodejs20-typescript或 nodejs22-typescript、在系統層級全域安裝 TS 編譯器,以及針對每個支援的Node.js版本個別安裝的方式。
tsc 不直接依賴任何Node.js版本。編譯器預期執行時間功能有特定層級,透過目標
Node.js 基於 的執行時間設計有一定的弱點:它在主機上僅支援一個版本的執行時間,並且需要專案層級所有相依性的重現性和一致性。這導致以下常見的 TypeScript 使用方法:TS 編譯器、目前Node.js版本的 TS 基礎組態,以及所有軟體相依性都安裝在專案內的本機。雖然全域安裝的節點模組預期只是 CLI 工具,例如 npm、tsc,這也是 CLI 工具,但很少全域安裝。值得注意的是,tsc 的全域 (全系統) 和本機 (專案內) 安裝可以共存,沒有問題,也可以是獨立使用的不同版本。請注意,本機安裝的 tsc 應使用與 npm 一起安裝的 npx 工具執行。因此,即使使用系統 TS 編譯器,使用者仍有機會選擇執行時間元件的版本,例如 Node.js(透過透過替代方案切換作用中版本)、TS 編譯器 (透過在本機安裝,或透過替代方案切換作用中版本),以及針對特定需求進行設定。
Amazon Linux 在每個Node.js版本基本上封裝 TS 編譯器的方式與其他全域安裝的節點模組相同,例如 npm。套件和二進位檔是命名空間,並包含 的主要版本Node.js做為其名稱的一部分。編譯器的預設可執行檔名稱 tsc 會在執行時間由替代工具管理,並指向目前作用中版本的 Node.js,其已停用且將由 執行。此選擇不會因目前的Node.js執行時間版本而減少。您可以讓節點可執行檔指向 Node.js 20,並將 tsc 設定為由 22 Node.js 解譯。您也可以獨立使用 TS 編譯器的命名空間名稱,例如 tsc-{MAJOR_VERSION},以設定預設 tsc 名稱。
管理 TS 編譯器作用中版本的一些實用命令
-
檢查針對 設定哪些替代方案
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