language
TypeScript
JavaScript に静的型付けを加えた言語。tsc コンパイラで JavaScript に変換して実行する。
問題一覧(18)
TypeScript で「Argument of type ... is not assignable」が解消できない
TS2345「Argument of type X is not assignable to parameter of type Y」は、関数引数の型 X が期待する型 Y のサブタイプでないために出るエラー。リテラル型は as const で保つ、null/undefined は型ガードで除外、オブジェクトは satisfies で検査、ジェネリックは型引数を明示、で大半は解決する。
更新: 2026-08-02、タグ: type-error、argument、assignability
TypeScript の as const で型が絞り込めない
as const はリテラル型と readonly 化を行うが、配置場所を間違えると効かない。配列やタプル全体に付ける必要があり、関数の戻り値で union 化を保ちたいなら戻り値式に付ける。要素単位の as const では狙った narrowing が起きない。
更新: 2026-08-02、タグ: as-const、narrowing、literal-type
TypeScript で「Cannot find module ... or its corresponding type declarations」が出る
ライブラリ本体が未インストール、@types/* パッケージが無い、moduleResolution の設定ミスのいずれかが原因。tsc が node_modules を辿るルールを押さえれば切り分けは早い。
更新: 2026-08-02、タグ: tsc、types、module
TypeScript で「can only be default-imported using the esModuleInterop flag」(TS1259) が消えない
CommonJS モジュールを default import で読むには esModuleInterop が要る。tsconfig で esModuleInterop を true にするか、import * as 形式へ書き換える。
更新: 2026-07-09、タグ: esmoduleinterop、import、commonjs
TypeScript でオブジェクトリテラルに余分なプロパティを指定できない
「Object literal may only specify known properties」はオブジェクトリテラルに対する厳密チェック。一度変数に代入する、型を拡張する、または index signature を加えると解消する。
更新: 2026-08-02、タグ: excess-property、type、object-literal
TypeScript で .js 拡張子を付けないと NodeNext の import が解決できない
moduleResolution: node16 / nodenext では ESM 仕様に従い相対 import に拡張子が必須。元ファイルが .ts でも import 側には .js と書くのが正解。バンドラ前提なら bundler に切り替えるのが楽。
更新: 2026-08-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-08-06、タグ: jsx、tsx、types
TypeScript で「Cannot use JSX unless the '--jsx' flag is provided」が解決できない
JSX を含む .tsx を扱うには tsconfig の jsx オプションが要る。未設定だと TS17004 が出るため、jsx を設定し拡張子を .tsx にすると解決する。
更新: 2026-07-29、タグ: jsx、tsconfig、react
TypeScript で「Not all code paths return a value」(TS7030) が消えない
noImplicitReturns が有効な関数で、値を返さない分岐があると出る。全分岐で return するか、戻り値型を明示して undefined を許容する。
更新: 2026-08-10、タグ: ts7030、noimplicitreturns、tsconfig
TypeScript で catch の「Object is of type 'unknown'」が解決できない
TypeScript 4.4 以降は useUnknownInCatchVariables(strict で既定有効)により catch の変数が unknown 型になる。現行版は TS18046「'e' is of type 'unknown'」、旧版は TS2571「Object is of type 'unknown'」として出る。instanceof や型ガードで絞ってからアクセスする。
更新: 2026-08-02、タグ: catch、unknown、error-handling
TypeScript で「Object is possibly 'null'」が解消できない
strictNullChecks 有効下では null/undefined を含む型のプロパティアクセスにナローイングが要る。早期 return / オプショナルチェイン / 非 null アサーションを場面ごとに使い分ける。
更新: 2026-08-02、タグ: strict、null、narrowing
TypeScript で「Parameter implicitly has an 'any' type」(TS7006) が解決できない
noImplicitAny が有効で、引数に型注釈が無く型推論もできないためです。引数へ明示的に型を付けるのが基本で、コールバックは文脈から型が付くよう書きます。
更新: 2026-08-02、タグ: noimplicitany、ts7006、any
TypeScript で「Property does not exist on type」が解消できない
Property 'x' does not exist on type 'Y' は実装より型定義が狭い時に出る。interface 拡張・型ガードでの絞り込み・宣言マージなど、原因に応じて使い分ける。
更新: 2026-08-02、タグ: property、type、interface
TypeScript で readonly 配列に push できず Property does not exist になり変更できない
readonly T[] / ReadonlyArray<T> / as const で得た配列はミューテーション系メソッド(push / pop / sort 等)の型を持たないため、変更しようとすると型エラーになります。スプレッドで可変コピーを作るか、可変な型に再宣言するのが基本対処です。
更新: 2026-08-02、タグ: readonly、array、as-const
TypeScript の satisfies 演算子で型が広がる・narrowing が効かない
satisfies は値が型に適合することを検査しつつ値の具体的な型は保持する演算子。as と違って型を広げないため narrowing も保たれるが、変数の型注釈や const アサーションと併用する位置を誤ると効果が消える。
更新: 2026-08-02、タグ: satisfies、narrowing、type-inference
tsc コマンドが見つからない(command not found: tsc)
tsc が command not found になる原因は、TypeScript をグローバルではなくプロジェクトにのみ入れているケースが大半。npx tsc か npm run 経由で叩くのが正解。
更新: 2026-08-02、タグ: tsc、command-not-found、path
TypeScript の tsconfig.json paths が実行時に解決できない
tsc は paths を「型解決」専用に使い、コンパイル後の JS にエイリアスを書き換えない。Node 直接実行や ts-node では tsconfig-paths などのローダ、bundler では別途 alias 設定が必要。
更新: 2026-08-06、タグ: tsconfig、paths、alias
TypeScript で「Type instantiation is excessively deep and possibly infinite」が解決できない
再帰条件型の展開が深くなりすぎると tsc は推論を諦めてこのエラーを出す。再帰の終端条件を明示する、中間結果を type alias に固定する、ユニオンの分配を制御するの 3 手で大半は通る。
更新: 2026-08-02、タグ: tsc、types、recursive-type
サンプルコード
エラーの解決ではなく「○○するには」の基本形を探しているときは、TypeScript のサンプルコード一覧(15件)を参照するとよい。