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

React+TypeScript项目ESLint与Prettier冲突警告如何消除?

解决React+TypeScript项目中ESLint与Prettier的逗号冲突警告

我配置了一个React.JS+TypeScript项目,集成了ESLint和Prettier,所有功能运行正常,但index.tsx文件中始终存在一条无法消失的红色警告:

警告截图

推测是ESLint与Prettier存在冲突,但无法理解为何会有要求在闭合括号后添加逗号的规则,需要解决方案消除警告。

以下是我的配置文件内容:

.eslintrc.json

{
  "env": {
    "browser": true,
    "es2021": true,
    "jest": true
  },
  "extends": [
    "react-app",
    "react-app/jest",
    "airbnb",
    "airbnb-typescript",
    "plugin:import/typescript",
    "plugin:prettier/recommended",
    "prettier"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module",
    "project": "./tsconfig.json"
  },
  "plugins": ["react", "@typescript-eslint", "prettier"],
  "rules": {
    "quotes": [
      "warn",
      "single",
      {
        "avoidEscape": true
      }
    ],
    "@typescript-eslint/quotes": [
      "warn",
      "single",
      {
        "avoidEscape": true
      }
    ],
    "react/jsx-uses-react": ["off"],
    "react/react-in-jsx-scope": ["off"],
    "react/jsx-props-no-spreading": ["warn"],
    "no-shadow": "off",
    "prettier/prettier": [
      "error",
      {
        "endOfLine": "auto"
      }
    ]
  }
}

.prettierrc

{
  "tabWidth": 2,
  "useTabs": false,
  "semi": true,
  "singleQuote": true,
  "trailingComma": "none",
  "printWidth": 80,
  "singleAttributePerLine": true,
  "arrowParens": "always",
  "bracketSameLine": false,
  "bracketSpacing": true,
  "endOfLine": "auto"
}

package.json

{
  "name": "devjam",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.5.2",
    "@types/node": "^16.18.23",
    "@types/react": "^18.0.32",
    "@types/react-dom": "^18.0.11",
    "@typescript-eslint/eslint-plugin": "^5.13.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.2.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-airbnb-typescript": "^17.0.0",
    "eslint-plugin-import": "^2.25.3",
    "eslint-plugin-jsx-a11y": "^6.5.1",
    "eslint-plugin-react": "^7.28.0",
    "eslint-plugin-react-hooks": "^4.3.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.10.0",
    "react-scripts": "5.0.1",
    "styled-components": "^5.3.9",
    "typescript": "^4.9.5",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "lint": "eslint .",
    "lint:fix": "eslint --fix ."
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-prettier": "^4.2.1",
    "prettier": "^2.8.7"
  }
}

解决方案

1. 定位冲突根源

警告来自Airbnb的ESLint配置——它强制要求尾随逗号(comma-dangle规则),但你的Prettier设置了"trailingComma": "none",两者规则冲突导致警告。

2. 调整ESLint规则,关闭尾随逗号强制要求

在.eslintrc.json的rules字段中添加以下配置,让ESLint不再强制尾随逗号,交由Prettier接管格式:

"comma-dangle": ["off"],
"@typescript-eslint/comma-dangle": ["off"]

3. 优化ESLint的extends顺序

plugin:prettier/recommended已经集成了eslint-config-prettier(自动关闭所有和Prettier冲突的ESLint规则)和eslint-plugin-prettier(将Prettier规则作为ESLint错误抛出),因此不需要单独添加"prettier"到extends数组中,避免规则覆盖顺序问题。修改后的extends数组如下:

"extends": [
  "react-app",
  "react-app/jest",
  "airbnb",
  "airbnb-typescript",
  "plugin:import/typescript",
  "plugin:prettier/recommended"
]

4. 自动修复现有问题

执行项目中的lint修复命令,让ESLint自动调整代码格式:

npm run lint:fix

5. 确保IDE插件协同工作

如果使用VS Code等编辑器,确保:

  • ESLint和Prettier插件已启用
  • 开启「保存时自动格式化」功能,设置编辑器优先使用ESLint进行格式化,避免手动格式冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 02:45:20