Eslint says all enums in Typescript app are "already declared in the upper scope"
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
see @typescript-eslint/no-shadow how to usealso this section of FAQ
How to use
{ // note you must disable the base rule as it can report incorrect errors "no-shadow": "off", "@typescript-eslint/no-shadow": ["error"]}
Searching typescript-eslint GitHub issues shows a number of people asking the same thing.
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"] },}
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 }] },}