プログラミング言語
TypeScript
JavaScript に静的型付けを加えた言語。tsc コンパイラで JavaScript に変換して実行する。
バージョン移行
メジャーバージョンを上げたときに動かなくなることを、旧版と新版で同じコマンドを打ち比べて確かめた。
TypeScript 6 → 7・変更点 8 つ
TypeScript を 6 から 7 に上げると動かなくなることTypeScript 5 → 6・変更点 10 つ
TypeScript を 5 から 6 に上げると動かなくなること
問題一覧(26)
TypeScript「Argument of type ... is not assignable」の原因と直し方
Argument of type X is not assignable は、as const・型ガード・satisfies で引数の型を絞れば消える。リテラル型の広がり、null の混入、ジェネリック推論の失敗が主な原因である。
更新: 2026-10-22、タグ: type-error、argument、assignability
TypeScript の as const で型が絞り込めない
as const はリテラル型と readonly 化を行うが、配置場所を間違えると効かない。配列やタプル全体に付ける必要があり、関数の戻り値で union 化を保ちたいなら戻り値式に付ける。要素単位の as const では狙った narrowing が起きない。
更新: 2026-10-22、タグ: as-const、narrowing、literal-type
TypeScript「Cannot find module ... or its corresponding type declarations」の原因と直し方
Cannot find module(TS2307)は、本体か @types/* を入れるか moduleResolution を直せば消える。tsc から本体か型定義が見えないときに出るので、どこで切れているかを順に確かめる。
更新: 2026-11-02、タグ: tsc、types、module
TypeScript「Cannot find name 'describe'」の原因と直し方
Cannot find name 'describe' は、tsconfig の types に "jest" か "vitest/globals" を足せば直る。TypeScript 6.0 は types の既定が空になり、@types/jest を入れても読まない。
更新: 2026-11-07、タグ: typescript-6、types、jest
TypeScript「Cannot find name 'process'」の原因と直し方
Cannot find name 'process' は、@types/node を入れて tsconfig の types に "node" を足せば直る。TypeScript 6.0 は types の既定が空になり、@types/node を入れても読まない。
更新: 2026-11-07、タグ: typescript-6、types、types-node
TypeScript「can only be default-imported using the esModuleInterop flag」の原因と直し方
can only be default-imported は、TypeScript 5.x なら esModuleInterop を true にすれば消える。6.0 以降は常に有効で、false が残っていると TS5107 / TS5108 で止まる。
更新: 2026-10-23、タグ: esmoduleinterop、import、commonjs
TypeScript でオブジェクトリテラルに余分なプロパティを指定できない
「Object literal may only specify known properties」はオブジェクトリテラルに対する厳密チェック。一度変数に代入する、型を拡張する、または index signature を加えると解消する。
更新: 2026-10-24、タグ: excess-property、type、object-literal
TypeScript で NodeNext の import に .js を付けないと解決されない
NodeNext で相対 import が解決されないときは、元が .ts でも .js 拡張子を書けば直る。ESM 仕様どおりの挙動で、バンドラ前提なら moduleResolution を bundler にするのが楽だ。
更新: 2026-11-02、タグ: esm、nodenext、module-resolution
TypeScript で TSX の JSX 要素が implicit any になる(TS7026)
JSX element implicitly has type 'any' は @types/react 未インストール、または tsconfig.json の jsx オプション未設定が原因。React なら @types/react を追加し jsx を react-jsx に指定する。
更新: 2026-11-02、タグ: jsx、tsx、types
TypeScript「Cannot use JSX unless the '--jsx' flag is provided」の原因と直し方
Cannot use JSX unless the '--jsx' flag is provided は、tsconfig の jsx を設定すれば直る。TS17004 として出る。JSX を含むファイルは拡張子も .tsx にする。
更新: 2026-10-25、タグ: jsx、tsconfig、react
TypeScript で「Not all code paths return a value」(TS7030) が消えない
Not all code paths return a value は、全分岐で return するか、戻り値型で undefined を許容すれば消える。noImplicitReturns が有効なときに出る。
更新: 2026-10-25、タグ: ts7030、noimplicitreturns、tsconfig
TypeScript「'e' is of type 'unknown'」が catch で出る原因と直し方
catch の 'e' is of type 'unknown'(TS18046)は、instanceof などで e を絞れば消える。TS 4.8 以前は Object is of type 'unknown' と表示された。
更新: 2026-10-26、タグ: catch、unknown、error-handling
TypeScript「Object is possibly 'null'」の原因と直し方
Object is possibly 'null' は、早期 return や ?. で null を除けば消える。strictNullChecks 下でナローイングせずにアクセスすると出る。! は最後の手段にする。
更新: 2026-10-29、タグ: strict、null、narrowing
TypeScript「Option 'baseUrl' has been removed」の原因と直し方
Option 'baseUrl' has been removed は、TypeScript 7.0 で消えた指定が tsconfig にあると出る。6.0 で非推奨になった指定で、ignoreDeprecations では黙らせられない。指定を消すか今の書き方に直せば通る。
更新: 2026-11-06、タグ: tsconfig、typescript-7、baseurl
TypeScript「Option 'baseUrl' is deprecated」の原因と直し方
Option 'baseUrl' is deprecated は、6.0 で非推奨になった指定が tsconfig にあると出る。今は "ignoreDeprecations": "6.0" で通るが、7.0 に備えて baseUrl を消し、paths を ./ 始まりに直す。
更新: 2026-11-07、タグ: tsconfig、typescript-6、baseurl
TypeScript「Parameter implicitly has an 'any' type」の原因と直し方
Parameter implicitly has an 'any' type は、引数に型注釈を付ければ消える。noImplicitAny が有効で型を推論できない引数に出る。コールバックは文脈から型が付くように書く。
更新: 2026-10-30、タグ: noimplicitany、ts7006、any
TypeScript「Property does not exist on type」の原因と直し方
Property 'x' does not exist on type 'Y' は、型定義を広げるか型ガードで絞り込めば消える。実装は知っているのに型が知らないプロパティにアクセスすると出る。
更新: 2026-11-05、タグ: property、type、interface
TypeScript で readonly 配列に push できない
readonly 配列に push できないときは、[...arr] や Array.from で可変コピーを作れば直る。readonly T[] や as const の配列は変更系メソッドを型に持たないためである。
更新: 2026-10-28、タグ: readonly、array、as-const
TypeScript「The 'rootDir' setting must be explicitly set」の原因と直し方
The 'rootDir' setting must be explicitly set は、tsconfig に "rootDir": "./src" を書けば直る。TypeScript 6.0 は rootDir の既定が変わり、出力が dist/src/ と 1 段深くなる。
更新: 2026-11-07、タグ: typescript-6、tsconfig、rootdir
TypeScript の satisfies 演算子で型が広がる・narrowing が効かない
satisfies は値が型に適合することを検査しつつ値の具体的な型は保持する演算子。as と違って型を広げないため narrowing も保たれるが、変数の型注釈や const アサーションと併用する位置を誤ると効果が消える。
更新: 2026-10-28、タグ: satisfies、narrowing、type-inference
TypeScript「type declarations for side-effect import」の原因と直し方
Cannot find module or type declarations for side-effect import は、*.css を宣言した .d.ts を置けば直る。TypeScript 6.0 は CSS の import も行き先を検査するようになった。
更新: 2026-11-07、タグ: typescript-6、css、side-effect-import
ts-node「reading 'fileExists'」が TypeScript 7 で出る原因と直し方
ts-node の (reading 'fileExists') という TypeError は、typescript@7 で起動すると出る。7.0 に ts-node が使う API が無いためで、Node.js 24 で直接動かすか、tsx に替えるか、6.0 を割り当てれば直る。
更新: 2026-11-06、タグ: ts-node、typescript-7、typeerror
tsc コマンドが見つからない(command not found: tsc)
tsc が command not found になる原因は、TypeScript をグローバルではなくプロジェクトにのみ入れているケースが大半。npx tsc か npm run 経由で叩くのが正解。
更新: 2026-10-29、タグ: tsc、command-not-found、path
TypeScript で tsconfig の paths エイリアスが実行時に解決されない
tsconfig の paths は実行時には効かず、tsconfig-paths などのローダか bundler の alias 設定を足せば直る。tsc はコンパイル後の JS の import を書き換えないためである。
更新: 2026-11-02、タグ: tsconfig、paths、alias
TypeScript「Type instantiation is excessively deep and possibly infinite」の原因と直し方
Type instantiation is excessively deep は、再帰型に終端条件を入れ、中間型を type で固定すれば通る。tsc が条件型の再帰展開を諦めたときに出る。
更新: 2026-10-30、タグ: tsc、types、recursive-type
TypeScript「TS5112: tsconfig.json is present」の原因と直し方
TS5112: tsconfig.json is present は、tsc にファイルを渡さず打つか、--ignoreConfig を付ければ直る。TypeScript 6.0 から、tsconfig.json のある場所で tsc index.ts と打つとエラーになった。
更新: 2026-11-17、タグ: typescript-6、tsc、tsconfig
サンプルコード
エラーの解決ではなく「○○するには」の基本形を探しているときは、TypeScript のサンプルコード一覧(85件)を参照するとよい。