Vue3 node 错误 error TS2345

文章目录

    • 错误出现
    • 错误原因分析
    • AI的解决方案
      • ️ **解决方案**
        • 1. **强制类型断言**
        • 2. **改用 `let` 声明数组**
        • 3. **更新 ESLint 类型声明**
        • 4. **检查导入语句**
        • 5. **降级 ESLint 配置方式(兼容方案)**
      • ⚠️ **验证与排查步骤**
      • **最佳实践建议**
      • ️ 我的解决方案
    • 运行结果

错误出现

之前的博客DeepSeek辅助写一个Vue3页面写了一个Vue3应用,运行npm run dev的时候功能正常,但当执行npm run build生成发布版的时候,却报错了:

transforming (7) node_modules\element-plus\es\components\alert\index.mjseslint.config.ts:19:3 - error TS2345: Argument of type ‘Config’ is not assignable to parameter of type ‘InfiniteDepthConfigWithExtendsAndVueSupport’.
Type ‘Config’ is not assignable to type ‘ConfigItemWithExtendsAndVueSupport’.
Types of property ‘languageOptions’ are incompatible.

使用DeepSeek,给出了以下错误原因,同时也给出一些解决方案,然而,我却发现——并没有什么用,仅错误原因分析稍有作用。

错误原因分析

TS2345 表示 函数参数类型与声明不匹配。结合上下文:

  1. globalIgnores 是 ESLint v9+ 的配置函数,其类型定义要求参数为 string[](字符串数组)。
  2. 若传入的值被 TypeScript 推断为其他类型(如 readonly string[] 或元组类型),或 ESLint 类型声明未正确加载,则触发此错误。
  3. 常见于以下场景:
    • 使用 const 定义的数组被推断为只读类型(readonly string[])。
    • ESLint 类型声明文件(@types/eslint)版本过旧,未兼容 v9 的 globalIgnores 类型。
    • 配置文件未正确导入 ESLint 类型模块。

AI的解决方案


解决方案

1. 强制类型断言

明确指定参数为 string[] 类型:

globalIgnores(['**/dist/**', '**/dist-ssr/**', '**/coverage/**'] as string[]);

适用场景:临时解决类型推断问题。

2. 改用 let 声明数组

避免 TypeScript 将数组推断为只读类型:

let ignorePatterns = ['**/dist/**', '**/dist-ssr/**', '**/coverage/**'];
globalIgnores(ignorePatterns);

适用场景:需复用忽略规则时。

3. 更新 ESLint 类型声明

确保安装兼容 ESLint v9 的类型包:

npm install --save-dev @types/eslint@latest

适用场景:类型声明过旧或缺失时。

4. 检查导入语句

确认配置文件中正确导入 globalIgnores

import { globalIgnores } from 'eslint/config'; // 必须从 'eslint/config' 导入

错误示例import globalIgnores from '...'(未解构导出)。

5. 降级 ESLint 配置方式(兼容方案)

若仍无法解决,改用传统 .eslintignore 文件:

# .eslintignore
dist/
dist-ssr/
coverage/

适用场景:放弃 v9 新配置,兼容旧版 ESLint。


⚠️ 验证与排查步骤

  1. 检查类型推断
    const patterns = ['**/dist/**'];
    type Check = typeof patterns; // 若为 `readonly string[]` 则需调整声明方式
    
  2. 确认 ESLint 版本
    // package.json
    "devDependencies": {
      "eslint": "^9.0.0" // 确保 ≥ v9.0
    }
    
  3. 查看类型声明文件
    定位 node_modules/@types/eslint/index.d.ts,搜索 globalIgnores,确认其定义为:
    declare function globalIgnores(patterns: string[]): ConfigItem;
    

最佳实践建议

方案 推荐度 适用场景
类型断言 (as string[]) ⭐⭐⭐⭐ 快速修复,临时使用
更新 @types/eslint ⭐⭐⭐⭐ 类型声明缺失或版本过旧
使用 .eslintignore ⭐⭐⭐ 兼容旧项目或放弃 v9 新配置
调整数组声明方式 (let) ⭐⭐ 需复用忽略规则时

优先通过 类型断言更新类型包 解决,若项目暂无法升级 ESLint v9,则采用 .eslintignore 方案。


以上方案仅供参考,在我的例子中并没有起作用。

️ 我的解决方案

修改eslint.config.ts文件:

原来的内容:

import { globalIgnores } from 'eslint/config'
import { defineConfigWithVueTs, vueTsConfigs } from '@vue/eslint-config-typescript'
import pluginVue from 'eslint-plugin-vue'

// To allow more languages other than `ts` in `.vue` files, uncomment the following lines:
// import { configureVueProject } from '@vue/eslint-config-typescript'
// configureVueProject({ scriptLangs: ['ts', 'tsx'] })
// More info at https://github.com/vuejs/eslint-config-typescript/#advanced-setup

export default defineConfigWithVueTs(
  {
    name: 'app/files-to-lint',
    files: ['**/*.{ts,mts,tsx,vue}'],
  },

  globalIgnores(['**/dist/**', '**/dist-ssr/**', '**/coverage/**']),

  pluginVue.configs['flat/essential'],
  vueTsConfigs.recommended,
)

报错的内容为globalIgnores所在行,修改为:

import { globalIgnores } from 'eslint/config'
import { defineConfigWithVueTs, vueTsConfigs } from '@vue/eslint-config-typescript'
import pluginVue from 'eslint-plugin-vue'

// To allow more languages other than `ts` in `.vue` files, uncomment the following lines:
// import { configureVueProject } from '@vue/eslint-config-typescript'
// configureVueProject({ scriptLangs: ['ts', 'tsx'] })
// More info at https://github.com/vuejs/eslint-config-typescript/#advanced-setup

export default defineConfigWithVueTs(
  {
    name: 'app/files-to-lint',
    files: ['**/*.{ts,mts,tsx,vue}'],
    ignores: ['**/dist/**', '**/dist-ssr/**', '**/coverage/**']
  },

  vueTsConfigs.recommended,
)

运行结果

Vue3 node 错误 error TS2345_第1张图片

你可能感兴趣的:(学海泛舟,javascript,typescript,开发语言,vue.js,前端)