Help us understand the problem. What is going on with this article?

VSCode + TypeScript の環境構築で "モジュール 〜.vue が見つかりません" とエラーが出る時の対処法

TypeScript開発環境を構築してる時にハマったので書いておきます。

エラー

Vueを使った開発中、コンパイルは問題ないのにVSCodeが下記のエラーを表示します。

モジュール 'foo.vue' が見つかりません

よくある対処

プロジェクトルートに下記内容の定義ファイル sfc.d.ts を作れば解決します。

sfc.d.ts
declare module '*.vue' {
  import Vue from 'vue';
  export default Vue;
}

今回の対処

しかしプロジェクトルートはただでさえ設定ファイルが多いので、これ以上増やしたくありません。@typesというフォルダを作り、そこにまとめます。

プロジェクトの一部
.
├── src
│   ├── @types
│   │   └── sfc.d.ts // ここに置いた
│   └── js
│       └── index.ts
└── tsconfig.json

tsconfig.json を編集

tsconfig.json
{
  "compilerOptions": {
 〜略〜
  "include": [
    "./src/js/**/*.ts",
    "./src/js/**/*.tsx",
    "./src/js/**/*.vue",
    "./src/@types/**/*.ts", // これを追加
    "tests/**/*.ts",
    "tests/**/*.tsx",
  ],
 〜略〜
}

tsconfig.json 全文
{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "typeRoots": [
      "./node_modules/@types",
    ],
    "types": [
      "webpack-env",
    ],
    "paths": {
      "@/*": ["./src/js/*"],
    },
    "lib": [
      "es2015",
      "es2015.promise",
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "./src/js/**/*.ts",
    "./src/js/**/*.tsx",
    "./src/js/**/*.vue",
    "./src/@types/**/*.ts",
    "tests/**/*.ts",
    "tests/**/*.tsx",
  ],
  "exclude": [
    "node_modules"
  ],
}
```</div>
</details>

sygnas
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした