LoginSignup
14
5

More than 1 year has passed since last update.

TypeScriptのファイルでVueファイルをimportしようとすると「Cannot find module '~~~~~' or its corresponding type declarations.」のエラーが出てしまう。

Last updated at Posted at 2022-06-08
App.vue
<template></template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
    setup() {
        
    },
})
</script>
main.ts
import { createApp } from "vue";
import App from "./App.vue";

createApp(App).mount("#app");

上記のようにmain.tsでApp.vueをimportしようとすると、main.tsの2行目「"./App.vue"」部分に

Cannot find module './App.vue' or its corresponding type declarations.

のエラーが出てしまう。

これはmain.tsがApp.vueをTypeScriptとして扱えないことが原因である。(よく考えると拡張子が異なるからそりゃ扱えないよね...)

解決法は、main.tsと同階層に「shims.d.ts」というファイル(VueファイルをTypeScriptとして認識させるためのファイル)を作成してあげればいい!

shims.d.ts
// コピペでOK!
declare module "*.vue" {
  import type { DefineComponent } from "vue";
  const component: DefineComponent<{}, {}, any>;
  export default component;
}

もし間違っている部分などございましたら、お手数ですがご教示いただければ幸いです!

14
5
1

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
14
5