You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么ESLint会对我的TypeScript文件中的Record类型报错?

问题解决步骤

1. 停止忽略.d.ts文件

你的ESLint配置里ignorePatterns包含了**/*.d.ts,这直接导致ESLint无法读取声明文件中的类型定义,是核心问题之一。把这一项从ignorePatterns中移除:

"ignorePatterns": [
    ".eslintrc.cjs",
    "**/*.html",
    "vite.config.js",
    "svelte.config.js",
    "libraries/",
    "node_modules/"
]

2. 调整ESLint扩展配置的顺序

ESLint扩展的顺序是后加载的配置会覆盖先加载的,你当前把airbnb-typescript/base放在@typescript-eslint/recommended前面,可能导致TS相关的基础规则被Airbnb的配置覆盖。调整顺序,把TS相关的扩展放在前面:

"extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking",
    "airbnb-typescript/base",
    "plugin:@ota-meshi/svelte/recommended",
    "plugin:import/recommended"
]

3. 确保tsconfig.json正确包含声明文件

检查你的tsconfig.json,确保include数组包含所有.d.ts文件,比如:

{
  "include": [
    "src/**/*",
    "**/*.d.ts"
  ]
}

如果你的声明文件在特定目录,也要明确加进去,避免TypeScript本身无法识别这些类型。

4. 验证依赖版本一致性

确保@typescript-eslint/parser、@typescript-eslint/eslint-plugin和你的typescript版本是兼容的。可以运行以下命令查看版本:

npm list @typescript-eslint/parser @typescript-eslint/eslint-plugin typescript

如果版本不匹配,升级到兼容的最新稳定版(比如TS 5.x搭配最新的@typescript-eslint v6.x)。

5. 清理ESLint缓存

有时候缓存会导致配置修改不生效,运行以下命令清理缓存:

npx eslint --clear-cache

做完这些步骤后,重启你的编辑器或ESLint服务,应该就能正常识别.d.ts中的类型和Record内置类型了。


内容的提问来源于stack exchange,提问作者Cookie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 06:35:26