资讯动态

typescript-eslint 报 “TSConfig does not include this file“ 错误怎么解决?

发布时间:2026/9/15 21:04:58 来源:尧图企业网站定制
typescript-eslint 报 TSConfig does not include this file 错误怎么解决【免费下载链接】typescript-eslint:sparkles: Monorepo for all the tooling which enables ESLint to support TypeScript项目地址: https://gitcode.com/GitHub_Trending/ty/typescript-eslint在启用类型感知 lintingtype-aware linting后如果你要 lint 的文件没有被任何 TSConfig 的include覆盖到typescript-eslint 会直接报错而不会静默跳过。这类报错的典型形式是ESLint was configured to run ... However, that TSConfig does not / none of those TSConfigs include this file它出现在使用parserOptions.project传统 project 模式或parserOptions.projectServicetypescript-eslint v8 起推荐的选项的项目中。本文的目标是对报错的每个文件做出忽略 / 降级 / 纳入 TSConfig三种处理之一然后重新运行 lint 确认报错消失。先对照报错消息判断你处于哪种模式文档区分了三种相关消息处理方式略有不同报错消息出现条件ESLint was configured to run ... However, that TSConfig does not / none of those TSConfigs include this fileESLint 配置在parserOptions.project中指定了 TSConfig但这些 TSConfig 没有覆盖到被 lint 的文件... was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject使用parserOptions.projectService文件不在其最近nearesttsconfig.json的include内也不在allowDefaultProject白名单里The file must be included in at least one of the projects provided你使用的是旧版typescript-eslint/parser升级到最新版本后会看到上面更具体的报错最常见的触发场景文档原话项目根目录有一个.eslintrc.cjs/eslint.config.mjs配置了parserOptions.project: true而tsconfig.json只写了include: [src]——ESLint 配置文件本身不在src下于是 IDE 或命令行 lint 该文件时就报这个错。判断方法很简单打开你的 ESLint 配置看parserOptions里用的是project还是projectService再对照上表选对应分支。按文件决定处理方式文档对每一个报错的文件给出同一个决策路径不想 lint 这个文件用 ESLint 自身的忽略机制把它排除。传统配置下是.eslintignore文件或ignorePatterns配置flat config 下是ignores配置键。例如把示例中的文件替换为你实际报错的文件// eslint.config.mjs示例把报错的配置文件排除在 lint 之外替换为你实际报错的文件 import { defineConfig } from eslint/config; export default defineConfig({ ignores: [eslint.config.mjs], // ... 其余配置 });想 lint 这个文件但不需要类型感知规则用disable-type-checked共享配置对该类文件单独关闭类型检查详见共享配置文档。flat config把files换成你实际要降级的文件模式**/*.js为文档示例值// eslint.config.mjs import { defineConfig } from eslint/config; import tseslint from typescript-eslint; export default defineConfig( // ... 其余配置 ... { files: [**/*.js], extends: [tseslint.configs.disableTypeChecked], }, );legacy 配置// .eslintrc.cjs module.exports { // ... 其余配置 ... overrides: [ { extends: [plugin:typescript-eslint/disable-type-checked], files: [./**/*.js], }, ], };注意disable-type-checked只关闭 typescript-eslint 自家的类型感知规则如果你还用了其他插件的类型感知规则需要手动关闭它们。想用类型感知规则 lint 这个文件必须让该文件进入某个 TSConfig 的include具体做法分两种模式见下一节。让文件进入 TSConfig按模式操作projectService 模式先改 include少量文件才用 allowDefaultProject首选把文件加入离它最近的tsconfig.json的include。文档给出的示例 diff允许.js文件// tsconfig.json include: [ src, *.js ]如果项目外文件数量很少可以设置projectService.allowDefaultProject允许这些文件用默认项目配置生成类型信息// eslint.config.mjs import { defineConfig } from eslint/config; export default defineConfig({ languageOptions: { parserOptions: { projectService: { allowDefaultProject: [*.js], // 替换为实际的项目外文件 glob不允许使用 ** }, }, }, });这个选项的限制来自 Parser 文档glob 中不允许**匹配allowDefaultProject的文件不得同时被其最近的tsconfig.json包含默认最多允许 8 个文件maximumDefaultProjectFileMatchCount_THIS_WILL_SLOW_DOWN_LINTING默认8每个文件都会触发一次新的 TypeScript program 构建有实打实的性能开销。如果你看到Having many files run with the default project is known to cause performance issues and slow down linting.的警告就是超过了这个默认上限——文档建议的对策同样是优先把文件加进最近tsconfig.json的include而不是扩大allowDefaultProject。每个文件的 glob 相对tsconfigRootDir解析allowDefaultProject之外还可以用defaultProject指定项目外文件使用的 TSConfig默认tsconfig.json。如果上面都不合适可以切到parserOptions.project获得更细粒度的控制见下节。project 模式检查 include或建一个专用的 tsconfig.eslint.json逐个检查你传给parserOptions.project的每个 TSConfig 的include——被 lint 的文件必须匹配其中某个includeglob。如果文件是.cjs、.js或.mjs确认 TSConfig 里开启了allowJsTSConfig 默认不包含.js文件。如果该文件本来就不该属于现有 tsconfig例如仓库本地脚本/工具文档建议在项目根创建一个tsconfig.eslint.json继承基础配置并把该文件列入include// tsconfig.eslint.json { // 继承基础配置避免重复定义 compilerOptions extends: ./tsconfig.json, include: [ src/**/*.ts, test/**/*.ts, typings/**/*.ts, // 按实际情况增删 // 混合 JS/TS 代码库别忘了包含 JS 文件 src/**/*.js ] }然后让parserOptions.project指向它// eslint.config.mjs export default defineConfig({ // ... languageOptions: { parserOptions: { project: ./tsconfig.eslint.json, }, }, // ... });// .eslintrc.js module.exports { // ... parserOptions: { project: ./tsconfig.eslint.json, tsconfigRootDir: __dirname, }, // ... };验证修复改完配置后重新运行你之前用的 lint 命令即可启用类型感知 linting 的文档中给出的命令形式是npx eslint .npx eslint .成功的判定之前报 TSConfig does not include this file 的那批文件不再出现该错误其余 lint 结果正常输出。如果你按升级路径处理的是旧版报错The file must be included in at least one of the projects provided升级typescript-eslint/parser后重跑应看到更具体的新版报错信息再按本文分支继续处理。限制与容易踩的边界projectService 只认tsconfig.json和jsconfig.json不会读tsconfig.eslint.json等相似命名的文件。所以 projectService 模式下想用单独 lint 配置只能走allowDefaultProjectdefaultProject这条路项目文档还建议反过来组织lint 用tsconfig.json保证编辑器里看到的类型和 lint 用的类型一致构建单独用tsconfig.build.json之类。同名不同扩展名会被 TypeScript 忽略同一目录下src/file.ts和src/file.js同时存在时TypeScript 只保留优先级最高的扩展名优先级从高到低.ts、.tsx、.d.ts、.js、.jsx其余文件即使includeglob 匹配上也不会被识别为项目内文件——这是 TSConfig does not include this file 的隐蔽成因之一见 Parser project。project和projectService不要同时开启会直接报错Enabling project does nothing when projectService is enabled. You can remove the project setting。启用 projectService 时建议直接删掉project配置项。project 模式下使用 project references 时TypeScript 不会自动跟随引用解析文件需要把每个被引用的 tsconfig 逐个或 glob加进project字段而 project references 本身只有parserOptions.projectService支持。使用宽泛的**glob 作为parserOptions.project值有性能代价文档建议尽量用单个*逐层匹配。如果你的项目是多包 monorepo报错的处理还会涉及多个包的 tsconfig 组织文档把这部分单列在 Troubleshooting Typed Linting Monorepos。更多选项细节tsconfigRootDir、extraFileExtensions等见 Parser 文档完整的类型感知 linting 问答见 Typed Linting 故障排查页。【免费下载链接】typescript-eslint:sparkles: Monorepo for all the tooling which enables ESLint to support TypeScript项目地址: https://gitcode.com/GitHub_Trending/ty/typescript-eslint创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价