📋 目錄
TypeScript 6.0 RC 發布了。這次升級的重點不是新功能,而是「清理技術債」——移除一些已經過時的目標(ES5、AMD/UMD/SystemJS),把 strict mode 變成預設值,以及為 TypeScript 7.0 的 Go 編譯器做準備。如果你現在還在用 TypeScript,這次升級你需要知道什麼?
為什麼 TypeScript 6.0 值得關注
TypeScript 6.0 不是一個「加新功能」的版本。它是一個「過渡版本」——這將是最後一版用 JavaScript 開發的 TypeScript 編譯器。TypeScript 7.0 將使用 Go 語言重寫,帶來「共享記憶體多執行緒」和「效能大幅提升」。
TypeScript 6.0 的任務,是「把 JavaScript 版本的 TypeScript 該處理的技術債處理乾淨」,讓未來的 Go 版本不需要再支援那些已經沒人用的舊特性。
這個版本對多數開發者的直接影響:Breaking Changes。
Strict-by-default:安全性的重大升級
什麼是 strict mode
TypeScript 的 strict mode 是一組更嚴格的型別檢查規則:
// 非 strict mode:這些不會報錯
let name: string = null; // null 不是 string,但允許
let age: number = undefined; // undefined 不是 number,但允許
function greet(name?: string) {
console.log(name.toUpperCase()); // 可選參數可能 undefined,會爆炸
}
// strict mode:這些全部報錯
TypeScript 6.0 的變化
// tsconfig.json
{
"compilerOptions": {
// TypeScript 6.0 之前:strict: false 是預設
// TypeScript 6.0:strict: true 是預設
"strict": true
}
}
這個變化的影響:升級 TS 6.0 後,你可能會突然發現一堆以前沒事的程式碼,現在開始報錯。
常見的 strict mode 錯誤
// 錯誤 1:null/undefined 不是允許的型別
let user: string = null; // TS 6.0 error: Type 'null' is not assignable to type 'string'
// 解決:
let user: string | null = null; // 明確宣告可為 null
// 錯誤 2:可選參數需要空值檢查
function greet(name?: string) {
console.log(name.toUpperCase()); // TS 6.0 error: 'name' is possibly 'undefined'
// 解決 1:
console.log(name?.toUpperCase());
// 解決 2:
if (name) {
console.log(name.toUpperCase());
}
}
// 錯誤 3:this 隱含 any
function handler() {
this.foo(); // TS 6.0 error: 'this' implicitly has type 'any'
// 解決:宣告 this 型別
function handler(this: { foo: () => void }) {
this.foo();
}
}
Explicit Resource Management:using 語法糖
什麼是 Explicit Resource Management
Explicit Resource Management 是 TC39 的一個 Stage 3 提案,TypeScript 6.0 搶先支援。它的核心概念很簡單:某些資源(如檔案、資料庫連線)在用完後,必須確保被關閉。
過去的寫法(需要手動 try/finally):
// 過去
function processFile() {
const handle = openFile('data.txt');
try {
// 使用 handle
const content = readFile(handle);
console.log(content);
} finally {
closeFile(handle); // 必須確保關閉,容易遺漏
}
}
現在的寫法(using 語法):
// TypeScript 6.0
function processFile() {
using handle = openFile('data.txt'); // using = 自動確保 closeFile() 被呼叫
const content = readFile(handle);
console.log(content);
} // <- 離開作用域時,自動 closeFile(handle)
using 關鍵字是 JavaScript TC39 Explicit Resource Management 提案的 TypeScript 版本。編譯器會自動在作用域結束時,呼叫資源的 [Symbol.dispose]() 方法。
實用範例
// 資料庫連線
async function queryUsers() {
using db = await createDatabaseConnection(DATABASE_URL);
const users = await db.query('SELECT * FROM users');
console.log(users);
} // <- 自動關閉連線
// HTTP 請求
async function fetchData() {
using response = await fetch('https://api.example.com/data');
const data = await response.json();
console.log(data);
} // <- 自動呼叫 response.body?.cancel()
// 計時器
function measureTime() {
using timer = createTimer();
// 做某些事情
expensiveOperation();
} // <- 自動停止計時器並輸出時間
這個功能特別適合:處理 async 資源或需要確保清理的場景,減少 memory leak 和資源遺漏。
Temporal API 支援
TypeScript 6.0 完整支援 TC39 Temporal API——一個比 Date 更好的日期時間處理方式。
// 過去:Date 的問題
const now = new Date();
const later = new Date(now.getTime() + 3600000);
// 1 小時後的時間,但 API 不直觀,時區處理複雜
// TypeScript 6.0:Temporal API
import { Temporal } from '@js-temporal/polyfill';
const now = Temporal.Now.instant();
const later = now.add({ hours: 1 });
// 格式化
const formatted = now.toLocaleString('zh-TW', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
});
// 時間差
const diff = later.until(now);
console.log(diff.total({ unit: 'minutes' })); // -60
Temporal API 的優勢:
- 不可變:所有 Temporal 物件都是 immutable,更容易推理
- 完整的時區支援:不需要手動處理時區偏移
- 清晰的 API:明確的物件類型(Instant、ZonedDateTime、PlainDate 等)
建置速度:40% 更快
TypeScript 6.0 RC 在建置速度上有顯著優化:
| 場景 | 速度提升 |
|---|---|
| 乾淨建置 | ~30-40% |
| Incremental 建置 | ~20-30% |
| 類型檢查 | ~25% |
這個提升主要來自編譯器内部的重構,以及更好的平行處理。對於大型 monorepo 專案,這個加速可以節省可觀的 CI/CD 時間。
Breaking Changes:升級前必須知道的事
1. ES5 target 移除
// tsconfig.json
{
"compilerOptions": {
// TypeScript 6.0:不再支援 target: "es5"
// 最低支援:ES2015(ES6)
}
}
影響:如果你的專案需要支援 IE 11 或更老的瀏覽器,升級 TS 6.0 後需要改變策略(建議使用 polyfill 或降級到 TS 5.x)。
2. AMD/UMD/SystemJS 移除
{
"compilerOptions": {
// TypeScript 6.0:不再支援
// "module": "amd"
// "module": "umd"
// "module": "system"
}
}
影響:如果你的專案還在用 RequireJS(AMD)或 SystemJS,這是需要處理的問題。現代前端幾乎都轉向 ES modules,這些模組格式在 2026 年已經很少使用。
3. Classic Module Resolution 移除
{
"compilerOptions": {
// TypeScript 6.0:不再支援 moduleResolution: "classic"
}
}
影響:使用 classic module resolution 的老舊專案需要切换到 node16 或 nodenext。
TypeScript 7.0 展望:Go 編譯器
TypeScript 團隊已經在開發 TypeScript 7.0,一個用 Go 語言重寫的編譯器:
| 維度 | TS 6.0(JS 編譯器) | TS 7.0(Go 編譯器) |
|---|---|---|
| 語言 | TypeScript/JavaScript | Go |
| 建置速度 | 基準 | 預計 5-10x 更快 |
| 多執行緒 | 受限於 JS 單執行緒 | 共享記憶體多執行緒 |
| 支援節點 | 6.0 RC | 開發中 |
TypeScript 7.0 的 Go 編譯器預計帶來「數量級」的效能提升,但 TS 6.0 的任務是「先把 JS 版本的技術債清理乾淨」,確保過渡平滑。
升級策略
升級步驟
# 1. 更新 TypeScript
npm install typescript@6.0-rc --save-dev
# 2. 執行型別檢查,看看有哪些錯誤
npx tsc --noEmit
# 3. 處理 strict mode 錯誤
# 建議使用 @typescript(strict: true) 的 escape hatch 讓錯誤可以逐步修復
# 4. 如果有 ES5 target,考慮升級到 ES2015+ 或降級
需要降級的場景
如果你的專案:
- 需要支援 IE 11
- 還在使用 AMD/UMD/SystemJS
- 有大量第三方套件不支援 strict mode
建議繼續使用 TypeScript 5.x,等待生態系追趕上 TS 6.0。
結語:清理是為了更好的未來
TypeScript 6.0 是一個「打掃房間」的版本。把那些已經沒人用的 ES5、AMD/UMD/SystemJS 清理掉,把 strict mode 變成預設值,讓 TypeScript 7.0 的 Go 編譯器有一個乾淨的基礎。
對多數團隊來說,這次升級需要一些工作:處理 strict mode 錯誤、移除老舊的 module 設定、更新ployfill。但這些工作是一次性的,代價是未來更快的編譯速度(TS 7.0)和更安全的預設值。
如果你正在用 TypeScript,現在是開始測試 6.0 RC 的時機。如果你還沒升級到 5.x,先升級到 5.x 再规划 6.0 遷移。
延伸閱讀
- TypeScript Strict Mode 完整指南 — strict mode 的詳細設定教學
本文是「2026 TypeScript 生態」系列文章之一。