如何在 NextJs 项目中添加 typescript 规则而不丢失 next/core-web-vitals?

Lar*_*ger 5 javascript typescript reactjs eslint next.js

我有一个打字稿 NextJS 项目,其中包含以下内容.eslintrc.json

{
    "extends": "next/core-web-vitals"
}
Run Code Online (Sandbox Code Playgroud)

我想添加一些额外的打字稿规则(例如不允许输入 any ( no-explicit-any))。

  1. 我添加了npm install --save-dev @typescript-eslint/parser @typescript-eslint/eslint-plugin
  2. 按规则扩展配置no-explicit-any,更新解析器并添加插件。
{
    "extends": "next/core-web-vitals",
    "parser": "@typescript-eslint/parser",
    "plugins": [
        "@typescript-eslint"
    ],
    "rules": {
        "@typescript-eslint/no-explicit-any": "warn"
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 现在no-explicit-any作品。然而,所有默认规则都next/core-web-vitals不再起作用。

所以我可以使用下一个或使用 typescript-eslint 规则。如何使用展位?

Sop*_*ert 1

您应该能够将两个预设包含在单个属性的数组中"extends",类似于:

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["react", "@typescript-eslint"],
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking",
    "next/core-web-vitals"
  ],
  "parserOptions": {
    "project": "./tsconfig.json",
  }
}
Run Code Online (Sandbox Code Playgroud)