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

Ada*_*mes 74 node.js typescript eslint

启动一个新应用程序,我安装了 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
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Tad*_*sen 91

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


看到@打字稿,eslint /无阴影如何使用 也是常见问题本节

如何使用

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

搜索typescript-eslint GitHub 问题会发现很多人都在问同样的问题。

  • 有遇到过这种情况的人可以告诉我你正在使用什么配置吗?如果有一个常用的公共配置滥用了此规则,我想让他们知道。 (5认同)
  • 当你使用 CLI 将 Angular 从 tslint 升级到 eslint 时,我使用默认配置 (2认同)

lib*_*iby 29

Tadhg McDonald-Jensen 的回答很有用,但有一点需要说明。直接写入以下配置项.eslintrc会报错:

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

这是一个带有无阴影规则的正确示例:

{
  "rules": {
      "no-shadow": "off",
      "@typescript-eslint/no-shadow": ["error"]
  },
}
Run Code Online (Sandbox Code Playgroud)


Kos*_*aft 12

我在 TypeScript 中使用以下代码时遇到了类似的问题:

\n
export enum MyEnum {\n  myValueOne = \'myValue\',\n  myValueTwo = \'myValueTwo\', // <-- got "already declared in the upper scope\xe2\x80\x9d error\n}\n\nexport class myValueTwo {\n   constructor(){}\n}\n
Run Code Online (Sandbox Code Playgroud)\n

不幸的是rules,两者都overrides没有解决问题

\n
 {\n   "rules": {\n      "no-shadow": "off",\n      "@typescript-eslint/no-shadow": ["error"]\n   },\n   "overrides": {\n      "no-shadow": "off",\n      "@typescript-eslint/no-shadow": ["error"]\n   },\n}\n
Run Code Online (Sandbox Code Playgroud)\n

在花了几个小时检查不同的问题、问题和有关该问题的文档后,我发现了@typescript-eslint/no-shadow. 链接在这里

\n

我要做的就是ignoreTypeValueShadow在 eslint for 中添加额外的选项@typescript-eslint/no-shadow

\n

我的无阴影最终设置如下所示:

\n
{\n  "overrides": {\n    "no-shadow": "off",\n    "@typescript-eslint/no-shadow": ["error", , { "ignoreTypeValueShadow": true }]\n  },\n}\n
Run Code Online (Sandbox Code Playgroud)\n