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

Angular 14项目中如何运行Lint自动修复?

Angular 14中Lint自动修复的正确操作方式

我使用Angular 14及对应版本的Lint工具,package.json中的scripts配置片段如下:

"name": "my-project",
"version": "0.0.0",
"scripts": {
  "ng": "ng",
  "start": "ng serve",
  ...
  "lint": "ng lint"
},

执行npm run lint时,触发如下错误:

42:10  error    Type number trivially inferred from a number literal, remove type annotation  @typescript-eslint/no-inferrable-types

对应的代码行是:

run(myParam: number = 0): Observable<any> {

该错误提示支持自动修复,但执行npm run lint --fix后代码无任何变化,终端仍提示:

2 errors and 0 warnings potentially fixable with the `--fix` option.

请问Angular中运行Lint自动修复的正确方式是什么?


解决方案

  • 使用正确的npm参数传递格式
    npm脚本中传递参数需要通过--分隔npm命令和目标脚本的参数,正确命令应为:

    npm run lint -- --fix
    

    直接写npm run lint --fix时,--fix会被识别为npm自身的参数,而非传递给ng lint的指令,导致修复操作不生效。

  • 直接调用Angular CLI命令
    跳过npm脚本,直接执行Angular CLI的lint修复命令,避免参数传递问题:

    ng lint --fix
    
  • 确认规则的可修复性
    检查项目根目录的.eslintrc.json配置文件,确认@typescript-eslint/no-inferrable-types规则未被手动设置为fix: false(该规则默认支持自动修复)。

  • 清理CLI缓存(可选)
    如果上述方法仍无效,尝试清理Angular CLI缓存后再执行修复:

    ng cache clean
    ng lint --fix
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 23:20:01