10月20日、Microsoft DevBlogsが「Announcing TypeScript 7.1 Beta」と題した記事を公開した。TypeScript 7.1 Betaの新機能と、特に既存ツールへの影響が大きい新コンパイラAPIの詳細が紹介されている。
TypeScript 7.1の最大のトピックは、コンパイラAPIの全面刷新だ。その背景にあるのが、Microsoftが進める「TypeScript Native Implementations(tsgo)」プロジェクトだ。TypeScriptのコンパイラをGoで書き直すことで処理速度を大幅に向上させるこの取り組みは、2025年初頭に発表されて以来コミュニティの注目を集めてきた。TypeScript 7.xはその成果が反映されるメジャーバージョン系列であり、7.1 BetaはGoランタイムを内包した最初のベータ版に位置づけられる。
GoランタイムとJavaScriptランタイムをまたいで動作するという新アーキテクチャにより、既存のAPIでは対応できない問題が生じた。それがメモリ管理だ。JavaScriptのGCはGoサイドのメモリを把握できないため、APIインスタンスを明示的に破棄しなければGoサイドでメモリリークが発生する。この課題に対してTypeScript 7.1が採用したのが、JavaScriptの比較的新しい構文であるExplicit Resource Management(usingキーワード)だ。ESLintやts-morphのようにTypeScriptのコンパイラAPIを直接利用しているツールは、この新APIへの対応が必要になる。
最大の変更点:新しいコンパイラAPI
新しいAPIは従来のモノリシックな設計を廃し、複数のエントリーポイントに分割された。
| エントリーポイント | 用途 |
|---|---|
typescript/sync |
同期型コンパイラクライアント(型チェッカー・program API含む) |
typescript/async |
Promiseベースの非同期版 |
typescript/fs |
リクエストスコープのファイルシステム |
typescript/ast |
AST型・SyntaxKind等の列挙型 |
typescript/ast/is |
ノード型ガード |
typescript/ast/factory |
create* / update* 関数群 |
以下は新APIを使ったシンプルなコンパイルの例だ。
import { API, formatDiagnosticsWithColorAndContext, type Diagnostic } from "typescript/sync";
using api = new API();
const reportErrors = (diagnostics: readonly Diagnostic[]) => {
console.log(formatDiagnosticsWithColorAndContext(diagnostics, api))
};
const commandLineArgs = process.argv.slice(2);
const parsedCommandLine = api.parseCommandLine(commandLineArgs);
if (parsedCommandLine.errors.length > 0) {
reportErrors(parsedCommandLine.errors);
process.exit();
}
using program = api.createProgram(parsedCommandLine.fileNames, parsedCommandLine.options);
const diagnostics = [
...program.getGlobalDiagnostics(),
...program.getProgramDiagnostics(),
...program.getSyntacticDiagnostics(),
...program.getSemanticDiagnostics(),
];
if (diagnostics.length > 0) {
reportErrors(diagnostics);
}
const emitResult = program.emit();
if (emitResult.diagnostics.length > 0) {
reportErrors(emitResult.diagnostics);
}
using api = new API() と using program = ... の箇所がポイントだ。usingはスコープを抜けた際に自動でリソースを解放する構文で、明示的に破棄したい場合は api.close() / program.dispose() を呼ぶこともできる。なお、ベータ版は以下のコマンドでインストールできる。
npm install -D typescript@beta
アンビエントモジュール宣言でImport Attributesをサポート
Import Attributes(import ... with { type: "..." } の構文)は、ランタイムやバンドラーにインポートの解釈方法を伝えるメタデータだ。
import myData from "./data/someData.json" with { type: "json" };
これまでTypeScriptでは、ワイルドカードパターンを使ったアンビエントモジュール宣言(declare module "*.css" 等)でファイル種別ごとの型を定義していた。しかしこの方法には問題がある。
- Import Attributesを付け忘れてもエラーにならない
.txtと.mdのように、異なる拡張子で同じtype: "text"を指定するケースで宣言が重複する- バンドラーが「Import Attributesなし=CSS Modules」「
type: "css"あり=CSSStyleSheet」のように挙動を切り替えるケースに対応できない
TypeScript 7.1では、アンビエントモジュール宣言にImport Attributesを含められるようになった。
declare module "*" with { type: "css" } {
const content: CSSStyleSheet;
export default content;
}
declare module "*" with { type: "text" } {
const content: string;
export default content;
}
declare module "*" with { type: "bytes" } {
const content: Uint8Array;
export default content;
}
これにより、Import Attributesの付け忘れをコンパイルエラーとして検出でき、属性に応じた正しい型が得られる。なお、これらの宣言はTypeScript組み込みライブラリには含まれない。ライブラリ作者はパッケージに含めることを避け、エンドユーザーが自プロジェクトで宣言する形が推奨される。
Source Phase Importsのサポート
Source phase importsはECMAScript Stage 3の提案機能で、import source というキーワードを使う。
import source SuperFastThing from "./super-fast-thing.wasm";
通常のインポートとの違いは、モジュールがパース・解析された後、依存モジュールのロードが完了する前の状態でオブジェクトを受け取ることだ。WebAssemblyの場合、このオブジェクトは WebAssembly.Module のインスタンスになり、WebAssembly.instantiate に直接渡せる。WebAssemblyのソースインポートはNode.jsの最近のバージョンですでにサポートされている。
動的なソースインポートとして import.source() 式も利用できる。
const SuperFastThing = await import.source("./super-fast-thing.wasm");
ソースインポートはアイデンティティを保持するため、Map や Set での利用や厳密等価比較が確実に動作する。使用には --module を esnext・nodenext・preserve のいずれかに設定する必要がある。
--target es2026の安定サポートとesnextライブラリの拡充
TypeScript 7.1では --target es2026 と --lib es2026 が安定版オプションとして追加された。lib.es2026 に含まれる主なAPIは以下のとおりだ。
Math.sumPrecise()Iterator.concat()JSON.rawJSON()Error.isError()Map.prototype.getOrInsert()/getOrInsertComputed()WeakMap.prototype.getOrInsert()/getOrInsertComputed()Uint8Array.prototype.toBase64()/setFromBase64()/toHex()/setFromHex()Array.fromAsync()
また、--lib esnext にはECMAScript提案中の以下のメソッドも追加された。
Promise.allKeyed/Promise.allSettledKeyedIterator.zip/Iterator.zipKeyedIterator.prototype.chunksIterator.prototype.joinIterator.prototype.includes
詳細はAnnouncing TypeScript 7.1 Betaを参照していただきたい。