Eslint 说 Typescript 应用程序中的所有枚举“已经在上层范围内声明”

新手上路,请多包涵

启动一个新的应用程序,我安装了 eslint 并使用以下配置对其进行了配置,但是每次我创建一个 enum 它都说它已经被定义了。甚至是无意义的字符串。其他变量类型(const、var、let)没有这个问题。我可以禁用该规则,但我希望它适用于它实际上是真的情况。

     {
  "root": true,
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "parserOptions": {
    "project": ["./tsconfig.json"],
    "ecmaFeatures": {
      "ecmaVersion": 6,
      "jsx": true
    }
  },
  "overrides": [],
  "extends": [
    "airbnb-typescript",
    "prettier",
    "prettier/@typescript-eslint",
    "plugin:@typescript-eslint/recommended-requiring-type-checking"
  ],
  "rules": {
    "spaced-comment": 0,
    "import/prefer-default-export": 0,
    "@typescript-eslint/no-use-before-define": 0,
    "@typescript-eslint/restrict-template-expressions": [
      1,
      { "allowBoolean": true }
    ],
    "react/jsx-props-no-spreading": "off",
    "react/state-in-constructor": 0,
    "react/require-default-props": 0,
    "react/destructuring-assignment": [
      1,
      "always",
      {
        "ignoreClassFields": true
      }
    ]
  }
}

在此处输入图像描述

原文由 Adam James 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.4k
1 个回答

如果您是 TSLint-to-ESLint 的用户,这是 一个已修复的错误, 因此使用较新版本重新运行脚本也可以解决此问题,或者只需禁用 no-shadow 并启用 @typescript-eslint/no-shadow

如果您正在使用一些滥用规则的公共配置,那么一定要让他们知道,仍然遇到这种情况的人数有点惊人。


请参阅 @typescript-eslint/no-shadow 如何使用 常见问题解答的这一部分

如何使用

{
  // note you must disable the base rule as it can report incorrect errors
  "no-shadow": "off",
  "@typescript-eslint/no-shadow": ["error"]
}

搜索 typescript-eslint GitHub issues 显示有很多人在问同样的事情。

原文由 Tadhg McDonald-Jensen 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题