TypeScriptの進化が止まらない
TypeScriptは年に4回のリリースサイクルで着実に進化を続けている。2026年現在、TypeScript 5.x系は多くの実用的な新機能を追加してきた。
本記事では、日々のコーディングですぐに使える新機能を厳選して紹介する。
satisfies演算子 — 型チェックと型推論の両立
TypeScript 5.0で導入された satisfies は、変数が特定の型を満たすことを検証しつつ、リテラル型を保持する演算子だ。
// as constだと型は保持されるが、typoを検出できない
const config = {
port: 3000,
host: 'localhost',
deubg: true, // typo!でもエラーにならない
} as const;
// satisfiesなら型チェック + リテラル型保持
type Config = { port: number; host: string; debug: boolean };
const config = {
port: 3000,
host: 'localhost',
debug: true,
} satisfies Config;
// config.port の型は number ではなく 3000
| アプローチ | 型チェック | リテラル型保持 |
|---|---|---|
| 型注釈 (: Config) | ✅ | ❌ |
| as const | ❌ | ✅ |
| satisfies | ✅ | ✅ |
ECMAScript Decorators — 標準化されたデコレータ
TypeScript 5.0でStage 3のECMAScriptデコレータが正式サポートされた。旧来の experimentalDecorators とは異なる、標準仕様に基づくデコレータだ。
function log(originalMethod: any, context: ClassMethodDecoratorContext) {
return function (this: any, ...args: any[]) {
console.log(`Calling ${String(context.name)} with`, args);
return originalMethod.apply(this, args);
};
}
class UserService {
@log
getUser(id: string) {
return { id, name: 'John' };
}
}
新しいデコレータはランタイム標準に準拠しているため、将来的にブラウザやNode.jsがネイティブサポートした際にそのまま動作する。
const型パラメータ — ジェネリクスの推論を狭める
TypeScript 5.0で追加された const 型パラメータは、ジェネリック関数の引数をリテラル型として推論させる。
// constなし: T は string[] と推論される
function getRoutes<T extends readonly string[]>(routes: T): T {
return routes;
}
const r1 = getRoutes(['home', 'about']); // string[]
// constあり: T は readonly ["home", "about"] と推論される
function getRoutes<const T extends readonly string[]>(routes: T): T {
return routes;
}
const r2 = getRoutes(['home', 'about']); // readonly ["home", "about"]
ルーティング定義や設定オブジェクトで、リテラル型の恩恵を受けたい場面で活用できる。
Isolated Declarations — 並列ビルドの高速化
TypeScript 5.5で導入された isolatedDeclarations オプションは、.d.ts ファイルの生成をモジュール単位で独立して行えるようにする。
// tsconfig.json
{
"compilerOptions": {
"isolatedDeclarations": true
}
}
従来は型推論のためにプロジェクト全体を解析する必要があったが、このオプションにより、各ファイルが独立して .d.ts を生成できる。大規模プロジェクトのビルド時間が劇的に短縮される。
ただし、関数の戻り値型やexportされた変数の型を明示的に注釈する必要がある。コードの可読性向上にもつながるため、デメリットは小さい。
using宣言 — リソースの自動解放
TypeScript 5.2で追加された using 宣言は、ECMAScriptのExplicit Resource Management提案に対応。スコープ終了時にリソースを自動解放する。
function readConfig() {
using file = openFile('config.json'); // [Symbol.dispose]が呼ばれる
return JSON.parse(file.read());
// スコープ終了時に自動でfile.close()
}
データベース接続、ファイルハンドル、ロック——これまで try/finally で管理していたリソースのクリーンアップが簡潔になる。
Template Literal Types の実用パターン
TypeScript 4.1で導入されたTemplate Literal Typesは、文字列パターンを型レベルで表現する。実用的なパターンをいくつか紹介。
// ルートパラメータの型安全な抽出
type ExtractParams<T extends string> =
T extends `${infer _}:${infer Param}/${infer Rest}`
? Param | ExtractParams<Rest>
: T extends `${infer _}:${infer Param}`
? Param
: never;
type Params = ExtractParams<'/users/:id/posts/:postId'>;
// type Params = "id" | "postId"
// CSSカスタムプロパティの型安全なアクセス
type CSSVar = `--${string}`;
function setVar(name: CSSVar, value: string) {
document.documentElement.style.setProperty(name, value);
}
setVar('--primary-color', '#3b82f6'); // OK
setVar('primary-color', '#3b82f6'); // Error!
NoInfer — 型推論の制御
TypeScript 5.4で導入された NoInfer<T> ユーティリティ型は、特定の型パラメータの推論を無効化する。
function createFSM<S extends string>(config: {
initial: NoInfer<S>;
states: S[];
}) { /* ... */ }
createFSM({
initial: 'idle', // S は states から推論される
states: ['idle', 'loading', 'error'],
});
NoInfer がないと、initial の値も S の推論に影響してしまう。NoInfer で推論元を限定することで、意図した型の絞り込みが可能になる。
実践的な tsconfig.json(2026年推奨)
{
"compilerOptions": {
"target": "ES2024",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"isolatedDeclarations": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true
}
}
| オプション | 効果 |
|---|---|
noUncheckedIndexedAccess | 配列/オブジェクトのインデックスアクセスに undefined を含める |
exactOptionalPropertyTypes | ? プロパティに undefined の明示的な代入を禁止 |
verbatimModuleSyntax | import/export文をそのまま出力(ESM/CJS混在を防止) |
まとめ
TypeScriptの進化は、「より安全に、より高速に、より表現力豊かに」という一貫した方向性を持っている。satisfies、using、NoInfer——いずれも「既存のコードをより良く書ける」機能であり、学習コストに対するリターンが高い。
すべてを一度に導入する必要はない。まず satisfies と noUncheckedIndexedAccess から始めて、プロジェクトに合った機能を段階的に取り入れていくのが現実的なアプローチだ。