问题描述

在Vue3中使用ts语法引入我自己写的组件时,VSCode报错 “无法找到模块xxx的声明文件。xxx隐式拥有 "any" 类型。”

原因说明

目前找到的合理解释就是:ts不认识.vue这个类型,需要定义声明。

解决方案

在项目根目录下找到env.d.ts文件,这个文件定义类型声明,简单地说就是让ts认识各种类型,尤其是文件。所以我们需要将自定义vue的类型声明加入到env.d.ts文件末尾。

declare module '*.vue' {
    import { ComponentOptions } from 'vue'
    const componentOptions: ComponentOptions
    export default componentOptions
}

The best code is no code at all.