Tibi
Tibi

Reputation: 489

declaring enum in typescript gives me error

In a typescript file at line 26 I have the following code:

export enum ItemType {
  Case = 'Case',
  Study = 'Study',
  Project = 'Project',
  Item = 'Item',
}

I am using Visual Studio Code as the IDE. The linting gives me an error 'ItemType' is already declared in the upper scope on line 26 column 13.eslint(no-shadow)

The only place where ItemType is referenced is below in the same file in an interface definition:

export interface Item {
  // other fields here

  readonly type: ItemType;
}

Nowhere else in my project do I have a definition of anything called 'ItemType'. This is what makes understanding this error difficult. Why do I get this error, and how do I fix my code?

Upvotes: 2

Views: 3408

Answers (2)

Raul Špilev
Raul Špilev

Reputation: 378

You need to turn off the regular no-shadow eslint rule and replace it with the typescript-eslint one.

So add this to your eslint configuration:

...
"no-shadow": "off",
"@typescript-eslint/no-shadow": ["error"],
...

Upvotes: 6

Evana
Evana

Reputation: 1

I was able to replicate the issue with the @typescript-eslint/parser in a minimal env with your code. Not sure what linting-rules or styleguides you're using but adding the following rules to my eslintrc.js file did the trick

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

Upvotes: -1

Related Questions