如何配置TypeScript插件与解析器协同工作
解读
面试官抛出这个问题,并不是想听你背一段“npm i eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin”就完事。
在国内真实面试场景里,他更关注三件事:
- 你能否把“解析器(parser)”与“插件(plugin)”这两个概念分清楚,知道谁负责把 TS 源码变成 AST,谁负责基于 AST 写规则。
- 你能否给出一份“能直接落地到团队脚手架”的最小可运行配置,且把 parserOptions.project 指向 tsconfig.json,确保规则能拿到类型信息。
- 你能否解释清楚为什么 CI 里经常出现“Parsing error: Cannot read file xxx/tsconfig.json”,并给出根因与解法。
答不到这三层,基本会被追问“你确定这样配就能跑?”
知识点
- 解析器链:ESLint 默认只认识 ECMAScript,@typescript-eslint/parser 把 TS 语法编译成兼容的 ESTree AST,并额外注入 type 节点。
- 插件链:@typescript-eslint/eslint-plugin 提供“@typescript-eslint/*”开头的规则集,分为 recommended、recommended-requiring-type-checking、strict 三档。
- project 选项:parserOptions.project 一旦开启,parser 会按需调用 TypeScript Language Service,规则才能做跨文件类型推导;代价是速度下降,需要缓存。
- 文件范围:include/exclude 与 tsconfig.json 的 files 字段必须覆盖所有被 lint 的 .ts/.tsx,否则报“file not included in project”。
- 性能优化:国内大厂仓库 2W+ 文件时,会搭配 eslint-cache、tsconfig.eslint.json(只给 lint 用)、以及 yarn pnp 的 resolve 插件,避免 OOM。
- 版本对齐:parser、plugin、typescript 三者的 major 版本必须一致,否则“Visitor not found”或“TypeError: services.program.getTypeChecker is not a function”这类诡异报错会在 GitHub Actions 里爆炸。
答案
下面给出一份“可直接拷贝到 monorepo 子包”的最小配置,并逐行解释为什么这样写。
-
安装依赖(国内源示例,保证版本对齐)
pnpm add -D eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin @types/eslint__js -
在项目根创建 eslint.config.mjs(ESLint 9 扁平配置,国内新团队已逐步迁移)
import tseslint from '@typescript-eslint/eslint-plugin'; import tsparser from '@typescript-eslint/parser'; import js from '@eslint/js'; export default [ js.configs.recommended, // 先加载 JS 推荐规则 { files: ['**/*.{ts,tsx}'], languageOptions: { parser: tsparser, parserOptions: { project: './tsconfig.eslint.json', // 独立配置,避免把测试、构建脚本拉进来 ecmaVersion: 'latest', sourceType: 'module' } }, plugins: { '@typescript-eslint': tseslint }, rules: { ...tseslint.configs['recommended-type-checked'].rules, ...tseslint.configs.strict.rules, // 国内团队常见定制 '@typescript-eslint/no-explicit-any': 'warn', '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }] } }, // 关闭对 JS 文件的类型检查,避免 CI 里报“JS 文件不在 project 中” { files: ['**/*.js'], languageOptions: { parserOptions: { project: false } } } ]; -
tsconfig.eslint.json 只给 lint 用,减少解析范围
{ "extends": "./tsconfig.json", "include": ["src/**/*", "test/**/*", "*.config.{ts,js}"], "exclude": ["node_modules", "dist", "build"] } -
package.json 脚本
"scripts": { "lint": "eslint . --cache --cache-strategy content --max-warnings 0", "lint:fix": "eslint . --fix --cache --cache-strategy content" } -
常见坑速查表(面试时可直接口述)
- 报错 “Parsing error: Cannot read file xxx/tsconfig.json”
根因:eslint 执行路径与 project 字段解析路径不一致;解法:把 project 写成绝对路径或保证 cwd 正确。 - 报错 “Files were not included in any tsconfig”
根因:include 漏了文件;解法:tsconfig.eslint.json 显式 include,或在 overrides 里对 JS 文件关闭 project。 - CI 突然 OOM
根因:parserOptions.project 全量拉起 TypeScript 语言服务;解法:eslint-cache + 并发限制 + 拆分子包。
- 报错 “Parsing error: Cannot read file xxx/tsconfig.json”
拓展思考
- 如果团队还在 ESLint 8,如何写 .eslintrc.js 等价配置?关键点是把 parser 和 plugins 字段拆成两层,并用 overrides 区分 TS/JS。
- 在 pnpm monorepo 里,每个子包都有自己的 tsconfig.json,如何共享一份顶层 ESLint 配置?答案是在 eslint.config.mjs 里用
import { dirname, join } from 'node:path';动态拼接project: join(dirname(import.meta.url), 'tsconfig.json'),并配合 root: true 防止连锁查找。 - 当项目升级到 TypeScript 5.x 的 “moduleResolution: bundler” 后,@typescript-eslint/parser 需要 >=6.1.0,否则所有 import 语句会报“Unable to resolve path”;面试时可以反问面试官“咱们 TS 版本是多少?”,体现版本兼容性意识。
- 未来 ESLint 10 计划把 @typescript-eslint 官方规则合并进 core,作为候选人你可以主动提出“如果合并完成,我们的配置可以简化成一条 extends: eslint/ts-recommended,届时 CI 缓存策略也要重新压测”,展示技术前瞻性。