使用最新TypeScript与ESLint时遇规则配置及语法错误问题
ESLint配置错误及后续兼容性问题排查
初始问题:@typescript-eslint/restrict-plus-operands 规则配置无效
执行eslint .时出现如下错误:
Error: .eslintrc.js » eslint-config-standard-with-typescript: Configuration for rule "@typescript-eslint/restrict-plus-operands" is invalid: Value {"skipCompoundAssignments":false} should NOT have additional properties.
已在配置中禁用该规则,但问题未解决:
rules: { ... '@typescript-eslint/restrict-plus-operands': 'off', }
原因分析
核心问题是**@typescript-eslint包版本不兼容**:
@typescript-eslint/eslint-plugin为v5.62.0,而@typescript-eslint/parser为v6.4.1,跨大版本不一致eslint-config-standard-with-typescript@39.0.0依赖v6.x系列的@typescript-eslint包,v5.x的插件无法解析v6.x规则的新增配置项skipCompoundAssignments,导致即使手动禁用规则,基础配置加载时仍报错。
更新后问题:Node.js语法兼容性错误
将@typescript-eslint/eslint-plugin升级至v6.4.1后,出现新错误:
/node_modules/@typescript-eslint/parser/node_modules/@typescript-eslint/scope-manager/dist/referencer/ClassVisitor.js:123 withMethodDecorators ||= ^^^ SyntaxError: Unexpected token '||='
原因分析
||=是ES2021新增的逻辑赋值运算符,当前使用的Node.js v14.18.2不支持该语法。@typescript-eslint v6.x版本已放弃对Node.js v14的支持,内部代码使用了ES2021+特性,导致旧Node.js版本无法运行。
解决方案
方案1:升级Node.js(推荐)
@typescript-eslint v6.x要求Node.js版本≥16.0.0,升级后可解决语法兼容性问题,同时保持依赖版本一致:
- 升级Node.js到v16 LTS及以上版本(如v16.20.2)
- 确保
package.json中@typescript-eslint/eslint-plugin和@typescript-eslint/parser版本统一为v6.x,重新安装依赖:
npm install
方案2:降级依赖适配Node.js v14
若无法升级Node.js,需将@typescript-eslint相关包降级到v5.x,并匹配对应版本的eslint-config-standard-with-typescript:
- 修改
package.json中的依赖版本:
"@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/parser": "^5.62.0", "eslint-config-standard-with-typescript": "^34.0.1"
- 删除
node_modules和package-lock.json(或yarn.lock) - 重新安装依赖:
npm install
内容的提问来源于stack exchange,提问作者Jack
相关产品推荐
相关产品推荐

