できない.dev

TypeScript を 5 から 6 に上げると動かなくなること

TypeScript を 5 から 6 に上げると、types と strict の既定が変わって型エラーが増え、baseUrl などの非推奨の設定は TS5101・TS5107 で止まる。
5.9 と 6.0 で打ち比べた 10 の変更点と直し方をまとめる。

公開: 更新:

typescript@5 を入れたプロジェクトと、同じファイルのまま typescript@6 に上げたプロジェクトを並べ、同じコマンドを打ち比べた。
各節の出力は「v5 では」「v6 に上げると」「v6 で直すと」の 3 段で並ぶ。

6.0 は JavaScript で書かれた TypeScript の最後の版で、Go で書き直した 7.0 へ移るための橋渡しとして出た。
動かなくなる原因は 2 つに分かれる。
1 つは既定値や既定の挙動が変わったことで、前半の 7 節がこれに当たる。
もう 1 つは 7.0 で消える設定の非推奨で、baseUrl などを書いていると TS5101 か TS5107 で止まる。

既定値の変更は、tsconfig.json に書いていない設定にだけ効く。
5.9 の tsc --init が作る tsconfig.json は "types": [] と "strict": true を初めから書いている(TS5112 の節の出力)ので、このファイルから始めたプロジェクトはこの 2 つの影響を受けない。

非推奨のエラーは、"ignoreDeprecations": "6.0" を書けば 6.0 の間は出なくなる。
7.0 ではこの指定が効かず、同じ設定が TS5102 と TS5108 で止まる。
その続きは TypeScript を 6 から 7 に上げると動かなくなること で打ち比べている。

公式のアナウンスが挙げる次の変更は、このページでは打っていない。

  • target の既定が、最新の ECMAScript(6.0 では ES2025)になった。
  • すべてのコードを strict モードとして扱うようになり、"alwaysStrict": false は非推奨になった。
    await や static のような予約語を変数名に使ったコードは、名前を変える。
  • allowSyntheticDefaultImports も、esModuleInterop と同じく false が非推奨になった。
  • module の amd・umd・systemjs・none は非推奨、outFile は削除とされている。
    バンドラーに置き換える。
  • "moduleResolution": "classic" は削除された。
  • module Utils {} の形の namespace の宣言はエラーになった。
    namespace に書き換える。
  • JSON の import に付ける assert { type: "json" } はエラーになった。
    with に書き換える。
  • /// <reference no-default-lib="true" /> は効かなくなった。

types の既定が空になり @types/node が読まれなくなった

5.x は、tsconfig.json に types を書かなければ、node_modules/@types にある型定義をすべて読んでいた。
6.0 では既定が [] になり、@types/node や @types/jest を入れてあっても読まない。
process や describe のように、型定義がグローバルに足す名前が見つからなくなる。

v5 では

$ cat src/index.ts
import { readFileSync } from "fs";
 
const home = process.env.HOME;
console.log(home, readFileSync("package.json", "utf8").length);
$ cat src/sum.test.ts
describe("sum", () => {
  it("adds", () => {
    expect(1 + 2).toBe(3);
  });
});
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0

v6 に上げると

$ npx tsc -p .
src/index.ts(1,30): error TS2591: Cannot find name 'fs'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
src/index.ts(3,14): error TS2591: Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
src/sum.test.ts(1,1): error TS2593: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha` and then add 'jest' or 'mocha' to the types field in your tsconfig.
src/sum.test.ts(2,3): error TS2593: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha` and then add 'jest' or 'mocha' to the types field in your tsconfig.
src/sum.test.ts(3,5): error TS2304: Cannot find name 'expect'.
$ echo $?
2

v6 で直すと

$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "types": ["node", "jest"]
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0

fs の import も TS2591 になった。
fs というモジュールの型は、@types/node の中で宣言されているからである。
lodash のように名前を書いて import するパッケージの型は、types に並べなくても今までどおり読まれる。

types に、使う型定義の名前を並べる。
Vitest の describe を globals: true で使っているなら "vitest/globals" を足す。
"types": ["*"] と書けば、5.x と同じく node_modules/@types を全部読む。

strict が既定で有効になった

5.x は、strict を書かなければ緩い検査で通していた。
6.0 では既定が true になり、noImplicitAny や strictNullChecks などがまとめて有効になる。
型を書いていない引数は TS7006 になり、null になりうる値をそのまま使うと TS18047 になる。

v5 では

$ cat src/index.ts
function greet(name) {
  return "hello " + name;
}
 
const title = document.querySelector("h1");
console.log(greet("dekinai"), title.textContent);
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0

v6 に上げると

$ npx tsc -p .
src/index.ts(1,16): error TS7006: Parameter 'name' implicitly has an 'any' type.
src/index.ts(6,31): error TS18047: 'title' is possibly 'null'.
$ echo $?
2

v6 で直すと

$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "strict": false
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ cat src/index.ts
function greet(name: string) {
  return "hello " + name;
}
 
const title = document.querySelector("h1");
console.log(greet("dekinai"), title?.textContent);
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0

"strict": false を書けば、noImplicitAny や strictNullChecks は 5.x の既定と同じく無効に戻る。
ただし公式のアナウンスによると、6.0 はすべてのコードを strict モードとして扱うので、static のような予約語を変数名に使ったコードはこれでは通らない(打っていない)。
型を付けて直すなら、引数に型を書き、null を返しうる querySelector の結果は ?. で受ける。
直す箇所が多いときは、先に "strict": false で 6.0 に上げ、あとから 1 つずつ直す。

rootDir の既定が tsconfig.json のある場所になった

rootDir は、outDir の中にどの階層からディレクトリを写すかを決める設定である。
5.x は入力ファイルの共通の親(ここでは src)を使い、src/index.ts を dist/index.js に出していた。
6.0 では tsconfig.json のある場所が既定になり、出力が dist/src/index.js と 1 段深くなる。

v5 では

$ cat tsconfig.json
{
  "compilerOptions": {
    "outDir": "dist"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ find dist -type f
dist/index.js

v6 に上げると

$ npx tsc -p .
tsconfig.json(3,5): error TS5011: The common source directory of 'tsconfig.json' is './src'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout.
  Visit https://aka.ms/ts6 for migration information.
$ echo $?
2
$ find dist -type f
dist/src/index.js

v6 で直すと

$ cat tsconfig.json
{
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "./src"
  },
  "include": ["src"]
}
$ rm -rf dist
$ npx tsc -p .
$ echo $?
0
$ find dist -type f
dist/index.js

公式のアナウンスは出力の場所が変わることを書いているが、6.0.3 は TS5011 のエラーも出した。
終了コードは 2 でも、出力は dist/src/ に書き出されている。
同じ場所で 5.x が作った dist/index.js が残っていれば、package.json の main や起動スクリプトがそちらを指したままになる(このページでは 5.x と 6.0 を別のディレクトリで打ったので、残るところは見せていない)。

"rootDir": "./src" を書けば dist/index.js に戻る。
dist/src/ に 6.0 の出力が残るので、dist を消してから変換し直す。

module の既定が esnext になり出力が ESM になった

5.x は、module を書かなければ CommonJS の JavaScript を出力していた。
6.0 では既定が esnext になり、export をそのまま残した ESM の JavaScript を出す。
tsc はエラーを出さないので、出力を動かしたところで初めて気づく。

v5 では

$ cat tsconfig.json
{
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "./src"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ cat dist/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.site = void 0;
exports.site = "dekinai";
console.log(exports.site);
$ node dist/index.js
dekinai
$ echo $?
0

v6 に上げると

$ npx tsc -p .
$ echo $?
0
$ cat dist/index.js
export const site = "dekinai";
console.log(site);
$ npm pkg get type
"commonjs"
$ node dist/index.js
(node:423) Warning: Failed to load the ES module: /work/v6/module-default/dist/index.js. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/work/v6/module-default/dist/index.js:1
export const site = "dekinai";
^^^^^^
 
SyntaxError: Unexpected token 'export'
    at wrapSafe (node:internal/modules/cjs/loader:1861:18)
    at Module._compile (node:internal/modules/cjs/loader:1903:20)
    at Object..js (node:internal/modules/cjs/loader:2060:10)
    at Module.load (node:internal/modules/cjs/loader:1651:32)
    at Module._load (node:internal/modules/cjs/loader:1443:12)
    at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
    at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
    at node:internal/main/run_main_module:33:47
 
Node.js v24.22.2
$ echo $?
1

v6 で直すと

$ cat tsconfig.json
{
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "./src",
    "module": "commonjs"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ node dist/index.js
dekinai
$ echo $?
0

6.0 の出力は export をそのまま残していた。
このプロジェクトの package.json は、npm 11 の npm init -y が書いた "type": "commonjs" を持っている。
そのため Node.js は dist/index.js を CommonJS として読み、SyntaxError で止まった。
Node.js の警告も、"type": "module" を書くか拡張子を .mjs にするよう案内している。

"module": "commonjs" を書けば、5.x と同じ CommonJS の出力に戻る。
ESM のまま動かすなら "module": "nodenext" にし、package.json の type に出力を合わせる方法もある(打っていない)。

CSS などの副作用 import が型エラーになった

import "./style.css" のように値を受け取らない import を、副作用 import と呼ぶ。
5.x は、副作用 import の行き先が解決できなくても黙って通していた。
6.0 では noUncheckedSideEffectImports が既定で有効になり、型の宣言が見つからないと TS2882 になる。
バンドラーで CSS を読み込むプロジェクトで出やすい。

v5 では

$ cat src/main.ts
import "./style.css";
 
console.log("loaded");
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "module": "esnext",
    "moduleResolution": "bundler"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0

v6 に上げると

$ npx tsc -p .
src/main.ts(1,8): error TS2882: Cannot find module or type declarations for side-effect import of './style.css'.
$ echo $?
2

v6 で直すと

$ cat src/env.d.ts
declare module "*.css";
$ npx tsc -p .
$ echo $?
0
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "noUncheckedSideEffectImports": false
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0

declare module "*.css"; を書いた .d.ts を置けば、.css の import が通る。
.scss や画像を読み込むなら、拡張子ごとに 1 行ずつ足す。
*.css の宣言は .css で終わる名前すべてに当てはまるので、"./styel.css" のような綴りの誤りはこの宣言では見つからない(打っていない)。

"noUncheckedSideEffectImports": false を書けば 5.x と同じになる。
公式のアナウンスは、この既定の変更を副作用 import の綴りの誤りを見つけるためと説明している。
false にすると、.ts のファイルを指す副作用 import も検査しなくなる(どちらも打っていない)。

esModuleInterop が常に有効になった

module が commonjs のとき、5.x の esModuleInterop は書かなければ false だった。
この状態では、CommonJS のパッケージを import * as で読み、そのまま関数として呼べた。
6.0 では esModuleInterop が常に有効になり、書いていないプロジェクトでもこの呼び出しが TS2349 になる。
false を書いて戻そうとすると、今度は非推奨の TS5107 で止まる。
ここでは Express のミドルウェアの cors を使った。

v5 では

$ cat src/index.ts
import * as cors from "cors";
 
const middleware = cors({ origin: "https://dekinai.dev" });
console.log(typeof middleware);
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "module": "commonjs"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0

v6 に上げると

$ npx tsc -p .
src/index.ts(3,20): error TS2349: This expression is not callable.
  Type '{ default: <T extends e.CorsRequest = CorsRequest>(options?: CorsOptions | CorsOptionsDelegate<T> | undefined) => (req: T, res: { statusCode?: number | undefined; setHeader(key: string, value: string): any; end(): any; }, next: (err?: any) => any) => void; }' has no call signatures.
$ echo $?
2
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "module": "commonjs",
    "esModuleInterop": false
  },
  "include": ["src"]
}
$ npx tsc -p .
tsconfig.json(5,24): error TS5107: Option 'esModuleInterop=false' is deprecated and will stop functioning in TypeScript 7.0. Specify compilerOption '"ignoreDeprecations": "6.0"' to silence this error.
$ echo $?
2

v6 で直すと

$ cat src/index.ts
import cors from "cors";
 
const middleware = cors({ origin: "https://dekinai.dev" });
console.log(typeof middleware);
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "module": "commonjs"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0

import cors from "cors" のように既定の import で読む。
import * as で読んだものは名前空間のオブジェクトなので、esModuleInterop が有効だと関数として呼べない。
@types/cors のように、型定義が関数と namespace を合わせて export = で出しているパッケージで起きる。

tsconfig.json があると tsc にファイルを渡せなくなった

5.x は、tsconfig.json のあるディレクトリで tsc index.ts を打つと、tsconfig.json を読まずに index.ts だけを変換していた。
tsconfig.json に書いた設定は効かないまま、出力が作られていた。
6.0 では TS5112 で止まり、何も出力しない。
tsc --init で tsconfig.json を作り、続けて tsc index.ts を打つと出る。

v5 では

$ cat index.ts
const message: string = "hello";
console.log(message);
$ npx tsc --init
Created a new tsconfig.json
 
You can learn more at https://aka.ms/tsconfig
$ echo $?
0
$ grep -E '"(types|strict)"' tsconfig.json
    "types": [],
    // "types": ["node"],
    "strict": true,
$ npx tsc index.ts
$ echo $?
0
$ ls
index.js
index.ts
tsconfig.json
$ cat index.js
var message = "hello";
console.log(message);

v6 に上げると

$ npx tsc index.ts
error TS5112: tsconfig.json is present but will not be loaded if files are specified on commandline. Use '--ignoreConfig' to skip this error.
$ echo $?
1
$ ls
index.ts
tsconfig.json

v6 で直すと

$ npx tsc --ignoreConfig index.ts
$ echo $?
0
$ ls
index.js
index.ts
tsconfig.json
$ cat index.js
"use strict";
const message = "hello";
console.log(message);
$ npx tsc
$ echo $?
0
$ ls
index.d.ts
index.d.ts.map
index.js
index.js.map
index.ts
tsconfig.json

tsconfig.json の設定で変換するなら、ファイルを渡さずに npx tsc を打つ。
ここでは tsc --init の tsconfig.json の設定で、index.d.ts と、index.js.map・index.d.ts.map も出力された。

tsconfig.json を無視して 1 ファイルだけ変換するなら、エラーの文言のとおり --ignoreConfig を付ける。
このとき使われるのは 6.0 の既定値で、出力の JavaScript も 5.x とは変わった。
5.x の index.js は var で書かれていたが、6.0 では const のまま残り、先頭に "use strict"; が付いた。

baseUrl が非推奨になった

baseUrl は、import のパスを解決する起点を決める設定である。
6.0 で非推奨になり、書いてあると TS5101 で止まる。
エラーの文言のとおり "ignoreDeprecations": "6.0" を足せば、6.0 の間は今までどおり動く。

v5 では

$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "baseUrl": ".",
    "paths": { "@/*": ["src/*"] }
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "baseUrl": ".",
    "paths": { "@/*": ["src/*"] },
    "ignoreDeprecations": "6.0"
  },
  "include": ["src"]
}
$ npx tsc -p .
tsconfig.json(6,27): error TS5103: Invalid value for '--ignoreDeprecations'.
$ echo $?
2

v6 に上げると

$ npx tsc -p .
tsconfig.json(4,5): error TS5101: Option 'baseUrl' is deprecated and will stop functioning in TypeScript 7.0. Specify compilerOption '"ignoreDeprecations": "6.0"' to silence this error.
  Visit https://aka.ms/ts6 for migration information.
$ echo $?
2

v6 で直すと

$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "baseUrl": ".",
    "paths": { "@/*": ["src/*"] },
    "ignoreDeprecations": "6.0"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "paths": { "@/*": ["./src/*"] }
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ cat src/index.ts
import { add } from "lib/math";
 
console.log(add(1, 2));
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "baseUrl": "./src",
    "ignoreDeprecations": "6.0"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true
  },
  "include": ["src"]
}
$ npx tsc -p .
src/index.ts(1,21): error TS2307: Cannot find module 'lib/math' or its corresponding type declarations.
$ echo $?
2
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "paths": { "*": ["./src/*"] }
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0

"ignoreDeprecations": "6.0" は、5.x で読むと TS5103 になる。
5.x は "6.0" という値を知らないからである。
5.x と 6.0 が同じ tsconfig.json を読むモノレポや CI では、全部を 6.0 に上げてから書く。

baseUrl を消すときは、paths の行き先を tsconfig.json からの相対パス(./src/*)に書き直す。
baseUrl が "." 以外なら、その値を paths の行き先の前に足す。
公式の移行ガイドは、この書き換えをするスクリプト(andrewbranch/ts-fix-baseurl)を案内している。

"baseUrl": "./src" を書き、"lib/math" のように paths を通さない名前で import していたプロジェクトは、baseUrl を消すだけでは TS2307 になる。
公式のアナウンスが示すとおり、paths に "*": ["./src/*"] の受け皿を足すと通る。

7.0 では ignoreDeprecations が効かないので、先送りした baseUrl は 7.0 に上げる前に消す。

moduleResolution: node が非推奨になった

"moduleResolution": "node" は Node.js 10 の頃の解決方法である。
6.0 では node10 という名前で TS5107 になる。

v5 では

$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "module": "commonjs",
    "moduleResolution": "node"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "module": "commonjs",
    "moduleResolution": "bundler"
  },
  "include": ["src"]
}
$ npx tsc -p .
tsconfig.json(5,25): error TS5095: Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.
$ echo $?
2

v6 に上げると

$ npx tsc -p .
tsconfig.json(5,25): error TS5107: Option 'moduleResolution=node10' is deprecated and will stop functioning in TypeScript 7.0. Specify compilerOption '"ignoreDeprecations": "6.0"' to silence this error.
  Visit https://aka.ms/ts6 for migration information.
$ echo $?
2

v6 で直すと

$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "module": "commonjs",
    "moduleResolution": "bundler"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "module": "nodenext"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ npm pkg get type
"commonjs"
$ npm pkg set type=module
$ echo $?
0
$ npx tsc -p .
src/index.ts(1,21): error TS2835: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './lib/math.js'?
$ echo $?
2
$ cat src/index.ts
import { add } from "./lib/math.js";
 
console.log(add(1, 2));
$ npx tsc -p .
$ echo $?
0

バンドラーでまとめるプロジェクトは "moduleResolution": "bundler" にする。
5.x の bundler は module が es2015 以降のときしか使えず、commonjs と組み合わせると TS5095 になった。
6.0 ではこの組み合わせも通るので、module を変えずに moduleResolution だけを書き換えられる。

Node.js で直接動かすプロジェクトは "module": "nodenext" にする。
このとき moduleResolution も nodenext になることは、TS2835 の文言に出ている。
拡張子を省いた ./lib/math がそのまま通ったのは、package.json が npm 11 の npm init -y の書いた "type": "commonjs" だったからである。
"type": "module" に変えると TS2835 になり、import に .js を付けると通る。

target: es5 と downlevelIteration が非推奨になった

6.0 では "target": "es5" が TS5107 になる。
ES5 向けの出力で for...of などを書き直す downlevelIteration は、書いてあるだけで TS5101 になる。

v5 では

$ cat src/index.ts
const scores = new Map([["sato", 80], ["suzuki", 92]]);
 
for (const [name, score] of scores) {
  console.log(name, score);
}
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "target": "es5",
    "lib": ["es2015", "dom"],
    "downlevelIteration": true
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0

v6 に上げると

$ npx tsc -p .
tsconfig.json(4,15): error TS5107: Option 'target=ES5' is deprecated and will stop functioning in TypeScript 7.0. Specify compilerOption '"ignoreDeprecations": "6.0"' to silence this error.
tsconfig.json(6,5): error TS5101: Option 'downlevelIteration' is deprecated and will stop functioning in TypeScript 7.0. Specify compilerOption '"ignoreDeprecations": "6.0"' to silence this error.
$ echo $?
2

v6 で直すと

$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "target": "es2015"
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0

target を es2015 以上にし、downlevelIteration を消す。
公式のアナウンスによると、6.0 では target を書かないと最新の ECMAScript(6.0 では ES2025)が既定になる。
古いブラウザのために ES5 のコードが要るなら、TypeScript の出力を Babel や SWC で ES5 に変換する。

検証環境Node.js 24.22.2 / npm 11.19.1 / Python 3.11.2 / 2026-11-08 検証(ほか 5 件)
検証日
実行環境
node:24 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 24.22.2
  • npm 11.19.1
  • Python 3.11.2
  • Git 2.39.5
  • TypeScript(旧) 5.9.3
  • TypeScript(新) 6.0.3
  • @types/node 24.19.2
  • @types/jest 30.0.0

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。