174

Starting a new application, I installed eslint and configured it with the following configs, but every time I create an enum it says it had already been defined. Even nonsense strings. Other variable types (const, var, let) don't have this issue. I could disable the rule but I would like it applied for situations where it is actually true.

    {
  "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
      }
    ]
  }
}

enter image description here

Adam James
  • 3,833
  • 6
  • 28
  • 47
  • 5
    it's the base rule that is throwing the error, disable `no-shadow` and enable `@typescript-eslint/no-shadow`. see [this](https://github.com/typescript-eslint/typescript-eslint/issues/2552#issuecomment-691694839) and it's subsequent links. – Tadhg McDonald-Jensen Sep 18 '20 at 19:39
  • if `@typescript-eslint/no-shadow` gives you issues as well it's possible you are using an outdated version, see [this](https://github.com/typescript-eslint/typescript-eslint/issues/2570) if that comes up. – Tadhg McDonald-Jensen Sep 18 '20 at 19:42

6 Answers6

224

If you are a user of TSLint-to-ESLint this was a bug that has since been fixed so rerunning the script with a newer version would also fix the issue, or just disable the no-shadow and enable @typescript-eslint/no-shadow

If you are using some public config that is misusing the rule then be sure to let them know, the number of people still running into this is somewhat staggering.


see @typescript-eslint/no-shadow how to use also this section of FAQ

How to use

module.exports = {
  "rules": {
    // Note: you must disable the base rule as it can report incorrect errors
    "no-shadow": "off",
    "@typescript-eslint/no-shadow": "warn"
  }
};

Searching typescript-eslint GitHub issues shows a number of people asking the same thing.

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
  • 6
    Could someone who runs into this please tell me what configuration you are using? if there is a commonly used public config that is misusing this rule I'd like to let them know. – Tadhg McDonald-Jensen Oct 14 '20 at 14:06
  • 2
    im using the default config when u upgrade angular from tslint to eslint using the CLI – danday74 Jan 02 '21 at 23:56
  • Confirming that this works with .eslintrc.json auto-generated by TSLint-to-ESLint tooling in Angular 11. You have to add the lines above skipping `{` and `}` to `overrides.rules[0]` (first item in the array). I would post the full config here, but it will be messed up in the comment. If this explanation is not clear enough I can create a GitHub gist. – Andrei Sinitson Jan 03 '21 at 17:27
  • 1
    @TadhgMcDonald-Jensen: This is the step which produces problematic configuration fixable by the lines you specified above: https://github.com/angular-eslint/angular-eslint#step-2---run-the-convert-tslint-to-eslint-schematic-on-a-project – Andrei Sinitson Jan 03 '21 at 17:36
  • Can confirm, angular-eslint, the conversion from tslint to eslint, contains this issue. – greduan Oct 29 '21 at 06:42
43

Tadhg McDonald-Jensen's answer is useful, but there is one thing that needs to be said. Writing the following configuration items directly to .eslintrc will report an error:

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

Here's a correct example with the no-shadow rule:

{
  "rules": {
      "no-shadow": "off",
      "@typescript-eslint/no-shadow": ["error"]
  },
}
liby
  • 679
  • 6
  • 13
10

I had similar issue with the following code in TypeScript:

export enum MyEnum {
  myValueOne = 'myValue',
  myValueTwo = 'myValueTwo', // <-- got "already declared in the upper scope” error
}

export class myValueTwo {
   constructor(){}
}

Unfortunately neither rules or overrides didn't solve issue

 {
   "rules": {
      "no-shadow": "off",
      "@typescript-eslint/no-shadow": ["error"]
   },
   "overrides": {
      "no-shadow": "off",
      "@typescript-eslint/no-shadow": ["error"]
   },
}

After spending few hours checking different Issues, questions and documentation about the problem I came across the official docs of @typescript-eslint/no-shadow. Here is the link

What I had to do is do add additional the ignoreTypeValueShadow options in eslint for @typescript-eslint/no-shadow.

My final setup for no-shadow looks like this:

{
  "overrides": {
    "no-shadow": "off",
    "@typescript-eslint/no-shadow": ["error", , { "ignoreTypeValueShadow": true }]
  },
}
Kosmonaft
  • 1,286
  • 2
  • 17
  • 30
2

It seems like adding this to the base "rules" was not enough and I had to add it again under the overrides

# eslintrc.js
{
  "rules": { // Did not work here as intended
    "@typescript-eslint/dot-notation": "error",
    "no-shadow": "off",
  },
  "overrides": [
    {
      "files": [
          "*.ts"
      ],
      ...
      "rules": { // Here it worked
          "@typescript-eslint/dot-notation": "error",
          "no-shadow": "off",
      }
  ]
}
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
1

This error occurs for me when I declared a variable with the some name of the object. I forgot to put the variable name in lowarcase rather than uppercase which the name of the object. like TypeFile: TypeFile

Solution: To fix it just put the variable name in lowercase.

Example of code generating this Eslint error:

This is my Enum: type-file-model.ts

public enum TypeFichier {
XML, PDF, IMAGE, ZIP
}

This is my object model app-file-model.ts

import {TypeFile} from 'app/shared/model/enum/type-file.model';

export interface IAppFile {
  ...
  TypeFile?: TypeFile;
}

export class AppFile implements IAppFile{
  constructor(
    ...
    public TypeFile?: TypeFile
  ) {}
}
Salim Hamidi
  • 20,731
  • 1
  • 26
  • 31
-2

I managed to stop the error from appearing using the following configuration:

{
   "rules": {
      "no-shadow": "off",
      "@typescript-eslint/no-shadow": ["off"]
   }
}

Use "off" for both cases, as I notice a recurring pattern in all the examples I read, which use "off" in the first and "error" in the second. This makes me doubt that it is the correct way to do it, but I have not been able to avoid those errors in another way, not even using overwriting.

chalo
  • 1,019
  • 2
  • 14
  • 27
  • You're not avoiding the _errors_ this way, you're just ensuring that eslint doesn't report any to you. Those are two different rules, one (`no-shadow`) is a base js rule that doesn't work properly in TypeScript files - that's why you should turn it off _for Typescript files_. The other is from the `typescript-eslint` plugin, and it correctly identifies the error in Typescript files, so turn it on as an error or warning. – Mogsdad Mar 09 '22 at 04:08