LoginSignup
15
7

More than 3 years have passed since last update.

【Cannot find module ... or its corresponding type declarations.】 tsconfig.json がプロジェクトのルート直下にない場合

Last updated at Posted at 2020-09-20

事の発端

スクリーンショット 2020-09-19 22.44.49.png

inject の戻り値の型が正しく返らず unknown になってる。

スクリーンショット 2020-09-20 15.18.05.png

そういえば import する時にエラーを吐いてる。

Cannot find module ... or its corresponding type declarations

こいつが原因か?

しかし、なぜ Cannot find module になるか分からない...

原因

自分の場合だと Rails プロジェクトの上に frontend ディレクトリを作り、tsconfig.json を置いていたのでこのような状況に陥った。そして、プロジェクトのルート直下で VSCode を開いていたため、tsconfig.json の内容がうまく機能していなかった。

フォルダ構成は以下のような感じ。

.
├── Gemfile
├── Gemfile.lock
└── frontend
  └── tsconfig.json
  └── components
...

解決

frontend ディレクトリ配下で VSCode を開いても解決するのだが、ルート直下に tsconfig.json を置いても解決する。extend でファイルパスを指定することで、継承する tsconfig.json を指定できる。

tsconfig.json
{
  "extends": "./frontend/tsconfig.json"
}

参考リンク

15
7
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
15
7