0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【TypeScript】tscでtsconfig.jsonが無視される問題

Last updated at Posted at 2023-10-04

背景

Expressをインポートしたコードをtsc --noEmitで型チェックしようとしたところ、

> pnpm tsc --noEmit index.ts
...can only be default-imported using the 'esModuleInterop' flag

のエラーが出たためtsconfig.json

"esModuleInterop": true

を追加したが、依然としてエラーが解消されなかった。

原因

公式ドキュメント

When input files are specified on the command line, tsconfig.json files are ignored.
(コマンドラインで入力ファイルを指定するとtsconfig.jsonは無視されます。)

どうして・・・😢

解決策1(失敗)

ともかく原因は判明したので、tsconfig.json--projectオプションで明示的に指定してみます。

> pnpm tsc --noEmit --project tsconfig.json index.ts
error TS5042: Option 'project' cannot be mixed with source files on a command line.

Option 'project' cannot be mixed with source files on a command line.
(オプション「project」はソースファイルの指定と同時に使用できません)

無視するから指示してあげたのに、それすら許されない・・・😢?

解決策2(成功!だが嬉しくはない)

仕方がないのでコマンドラインで直接オプションを指定しました。

> pnpm tsc --noEmit --esModuleInterop index.ts
(エラーなし)

成功です(嬉しくはない)。

補足

lint-stagedとtscを併用する時に今回のエラーが発生する場合の解決方法はREADME.mdにありました👇
Example: Run tsc on changes to TypeScript files, but do not pass any filename arguments

まとめ

どうしてこんな面倒な仕様になっているんでしょうか・・・😢?

歴史的経緯などご存知の方がいればぜひコメントください・・・。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?