TypeScript 无法识别 .vue 文件的类型

原因

缺少对应的类型说明

解决前

001

解决后

002

解决步骤

在项目中创建一个全局的类型声明文件(如 shims-vue.d.ts),为 .vue 文件添加类型定义,使 TypeScript 正确识别 Vue 组件模块。

a. 在 src 目录下新建或修改类型声明文件 shims-vue.d.ts,内容如下:

declare module '*.vue' {
  import { DefineComponent } from 'vue'
  const component: DefineComponent<{}, {}, any>
  export default component
}

b. 确保 tsconfig.json 中包含对声明文件的支持,例如:

{
  "include": ["src/**/*"]
}
posted @ 2025-07-29 16:40  motley  阅读(59)  评论(0)    收藏  举报