Vite 项目中配置 vite-plugin-eslint 插件报错 Could not find a declaration file for module vite-plugin-eslint.

关于在 vite.config.ts 中引入 vite-plugin-eslint 插件,出现如下报错:
无法找到模块 vite-plugin-eslint 插件中的 TS 声明文件,隐式含有 “any” 类型。

Could not find a declaration file for module vite-plugin-eslint. 
xxxxx/node_modules/vite-plugin-eslint/dist/index.mjs 
implicitly has an any type.

image.png
其实,从报错信息后面的内容中:

There are types at xxxx/node_modules/vite-plugin-eslint/dist/index.d.ts, 
but this result could not be resolved when respecting package.json exports. 
The vite-plugin-eslint library may need to update its package.json or typings.

我们可以得到在该插件中是含有 TS 声明文件的,vite-plugin-eslint/dist/index.d.ts,但是由于 TypeScript 的变更,导致新版本的 typescript 与依赖包中 package.json 指明 TS 声明文件位置的 types 配置项不匹配,最终导致新版本的 TypeScript 找不到 vite-plugin-eslint 插件中的 TS 声明文件。
在新版的 TypeScript 中,已经不再使用 package.json 文件中根结构中的 types 字段指明 TS 声明文件位置,而是在 exprots 中相应的导入方式中添加 typs 字段指明 TS 声明文件位置。

官方文档:https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing

我这里的解决方法是,在 node_modules 中找到 vite-plugin-eslint 插件的 package.json 文件
image.png
将其中的 exports 字段修改为如下内容,让新版的 TypeScript 在使用 import 导入时能够找到 vite-plugin-eslint 插件中的 TS 声明文件。

  "exports": {
    ".": {
      "import": {
        "types": "./dist/index.d.ts",
        "default": "./dist/index.mjs"
      },
      "require": "./dist/index.js"
    }
  },

image.png
修改完成后,重启 IDE 就不报错了。
image.png

至于 https://blog.csdn.net/qq_41694633/article/details/136023251https://blog.csdn.net/qq_39404437/article/details/128675809 中给依赖打补丁的方法,emmmm,没看懂。