ts 如何忽略 node_module 中包的类型错误 ?

通过vite 构建项目, 会先执行 vue-tsc --noEmit 来进行 ts 检查类型

 "scripts": {
   "build": "vue-tsc --noEmit && vite build",
  },

但是node_module中有一个依赖的类型检查存在错误

yarn run v1.22.10
$ vue-tsc --noEmit && vite build
node_modules/vxe-table-plugin-element/index.ts:272:26 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

272       h(resolveComponent(name), {
                             ~~~~

node_modules/vxe-table-plugin-element/index.ts:309:35 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

309         return h(resolveComponent(name), {
                                      ~~~~

node_modules/vxe-table-plugin-element/index.ts:375:26 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

375       h(resolveComponent(name), {
                             ~~~~

node_modules/vxe-table-plugin-element/index.ts:428:39 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

428             return h(resolveComponent(name) as ComponentOptions, {
                                          ~~~~

node_modules/vxe-table-plugin-element/index.ts:694:41 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

694               return h(resolveComponent(name), {
                                            ~~~~

tsconfig.json 文件配置如下:

{
  "compilerOptions": {
    "target": "esnext",
    "useDefineForClassFields": true,
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "lib": ["esnext", "dom"],
    "skipLibCheck": true
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", "components.d.ts", "auto-imports.d.ts"],
  "exclude": ["node_modules/**"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

如何忽略这种报错呢?

补充:

package.json文件

{
  "name": "vxe-table-element-plus",
  "private": true,
  "version": "0.0.0",
  "scripts": {
    "dev": "vite",
    "build": "vue-tsc --noEmit --skipLibCheck && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "element-plus": "^2.2.9",
    "vue": "^3.2.37",
    "vxe-table": "^4.2.8",
    "vxe-table-plugin-element": "^3.0.5",
    "xe-utils": "^3.5.4"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^2.3.3",
    "typescript": "^4.7.4",
    "unplugin-auto-import": "^0.9.2",
    "unplugin-vue-components": "^0.21.1",
    "vite": "^2.9.14",
    "vite-plugin-dts": "^1.2.1",
    "vue-tsc": "^0.38.8"
  }
}

main.ts

import { createApp } from 'vue';


import VXETablePluginElement from 'vxe-table-plugin-element';
import 'vxe-table-plugin-element/dist/style.css';
import App from './App.vue';

VXETable.use(VXETablePluginElement);
createApp(App).mount('#app');
阅读 8.4k
2 个回答
2022-7-19 补充

我去建了个小项目试了下,确实有问题。这应该是 vxe-table-plugin-element 的问题,它把一个 TypeScript 源文件当作声明文件来使用。如果改成申明文件就好(如下图)

image.png

我觉得可以去提个 BUG。在这个问题修复之前,可以手工去修改一下 node_modules 中的东西,也可以写个小小的脚本来自动干这个事情。


原回答

试试给 vue-tsc 加 --skipLibCheck 这个参数

我是直接 "build": vite build

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题