如何将 typescript-eslint 规则添加到 eslint

How to add typescript-eslint rules to eslint

我正在使用 NX 构建 Angular 应用程序,并且我已将其配置为使用 ESLint。我们可以配置像 no-console 这样的内置规则,但是 @typescript-eslint/no-inferrable-types 不接受我们的覆盖。我们甚至尝试将规则移动到 overrides 部分的变量条目中,但没有任何效果。

我们在哪里指定typescript-eslint规则?

我们的根级别.eslintrc.json

{
  "root": true,
  "ignorePatterns": ["**/*"],
  "plugins": ["@nrwl/nx"],
  "overrides": [
    {
      "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
      "rules": {
        "@nrwl/nx/enforce-module-boundaries": [
          "error",
          {
            "enforceBuildableLibDependency": true,
            "allow": [],
            "depConstraints": [
              { "sourceTag": "*", "onlyDependOnLibsWithTags": ["*"] }
            ]
          }
        ]
      }
    },
    {
      "files": ["*.ts", "*.tsx"],
      "extends": ["plugin:@nrwl/nx/typescript"],
      "parserOptions": { "project": "./tsconfig.*?.json" },
      "rules": {}
    },
    {
      "files": ["*.js", "*.jsx"],
      "extends": ["plugin:@nrwl/nx/javascript"],
      "rules": {}
    }
  ],
  "rules": {
    "no-console":"warn",
    "@typescript-eslint/no-inferrable-types": "off"
  }
}

您的插件中缺少插件参考 array.So 只有“@nrwl/nx”规则,其他没有名称空间的规则将适用。 试试这个 : “插件”:[ "@nrwl/nx", “@typescript-eslint”]。 哦,如果您只想将其应用于某种文件(例如 .ts 或 .tsx 文件),您也可以将其添加到“覆盖”部分,or/and 更改处理解析器

根据我对 NX linting 工作原理的理解,我认为您可以将规则添加到覆盖部分。

在下面的 JSON 中,您可以看到 no-console 已添加到具有 *.ts 和 *.tsx 文件匹配项的覆盖数组元素的“规则”。

{
  "root": true,
  "ignorePatterns": ["**/*"],
  "plugins": ["@nrwl/nx"],
  "overrides": [
    {
      "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
      "rules": {
        "@nrwl/nx/enforce-module-boundaries": [
          "error",
          {
            "enforceBuildableLibDependency": true,
            "allow": [],
            "depConstraints": [
              { "sourceTag": "*", "onlyDependOnLibsWithTags": ["*"] }
            ]
          }
        ]
      }
    },
    {
      "files": ["*.ts", "*.tsx"],
      "extends": ["plugin:@nrwl/nx/typescript"],
      "parserOptions": { "project": "./tsconfig.*?.json" },
      "rules": {
        "no-console":"warn"
      }
    },
    {
      "files": ["*.js", "*.jsx"],
      "extends": ["plugin:@nrwl/nx/javascript"],
      "rules": {}
    }
  ],
  "rules": {
    "@typescript-eslint/no-inferrable-types": "off"
  }
}

重要的是哪个覆盖了你放入规则的数组元素。我遇到了类似的问题,你可以在 .

的答案中看到我尝试过的不同方法的完整解释

值得注意的是,在 pluginsextends.

中已经指定的压倒性规则似乎让人喜忧参半