typescript-eslint: Incorrectly triggers no-undef and no-redeclare in some cases
- I have tried restarting my IDE and the issue persists.
- I have updated to the latest version of the packages.
- I have read the FAQ and my problem is not listed.
Repro
https://github.com/otakustay/typescript-eslint-issue
Expected Result
No errors
Actual Result
as-const-undef.ts
4:6 error 'const' is not defined no-undef
generic-jsx-no-undef.tsx
12:19 error 'T' is not defined no-undef
type-assertion-no-undef.ts
6:43 error 'config' is not defined no-undef
type-var-redef.tsx
5:7 error 'Value' is already defined @typescript-eslint/no-redeclare
✖ 3 problems (3 errors, 0 warnings)
Additional Info
Versions
| package | version |
|---|---|
@typescript-eslint/parser |
4.0.1 |
TypeScript |
4.0.2 |
ESLint |
7.8.1 |
node |
14.7.0 |
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 15 (6 by maintainers)
Commits related to this issue
- feat(scope-manager): add support for analysing JSX Fixes #2455 And part of #2477 JSX is a first-class citizen of TS, so we should really support it as well. I was going to just rely upon `eslint-plu... — committed to typescript-eslint/typescript-eslint by bradzacher 6 years ago
- fix(scope-manager): add `const` as a global type variable Handles part of #2477 — committed to typescript-eslint/typescript-eslint by bradzacher 6 years ago
- feat(scope-manager): add support for JSX scope analysis (#2498) Fixes #2455 And part of #2477 JSX is a first-class citizen of TS, so we should really support it as well. I was going to just rely... — committed to typescript-eslint/typescript-eslint by bradzacher 6 years ago
- docs(eslint-plugin): [no-redeclare] clearly document type/variable redeclare case Addresses #2477 — committed to typescript-eslint/typescript-eslint by bradzacher 6 years ago
- docs(eslint-plugin): [no-redeclare] clearly document type/variable redeclare case (#2500) Addresses #2477 — committed to typescript-eslint/typescript-eslint by bradzacher 6 years ago
- fix(scope-manager): add `const` as a global type variable Handles part of #2477 — committed to typescript-eslint/typescript-eslint by bradzacher 6 years ago
- fix(scope-manager): add `const` as a global type variable Handles part of #2477 — committed to typescript-eslint/typescript-eslint by bradzacher 6 years ago
- fix(scope-manager): add `const` as a global type variable Handles part of #2477 — committed to typescript-eslint/typescript-eslint by bradzacher 6 years ago
- feat: Use @typescript-eslint version of no-shadow/no-redeclare Use @typescript-eslint versions of the no-shadow and no-redeclare rules. Remove the no-undef rule since that is covered by typescript a... — committed to Tigge/eslint-config-typescript-shareable by Tigge 6 years ago
- feat: use @typescript-eslint version of no-shadow/no-redeclare Use @typescript-eslint versions of the no-shadow and no-redeclare rules. Remove the no-undef rule since that is covered by typescript a... — committed to Tigge/eslint-config-typescript-shareable by Tigge 6 years ago
- feat: use @typescript-eslint version of no-shadow/no-redeclare Use @typescript-eslint versions of the no-shadow and no-redeclare rules. Remove the no-undef rule since that is covered by typescript a... — committed to AxisCommunications/eslint-config-typescript-shareable by Tigge 6 years ago
Pre v4 the scope analyser (this piece of infra which backs rules like
no-undef,no-shadowandno-unused-vars) essentially barely worked.The implementation was a hastily patched version of the core eslint scope analyser in an attempt to teach it about some common use cases for types, but it was a bad approach to solve the problem because typescript’s types scope is super complex. There were many bugs, and it sucked.
For v4 I forked the core eslint scope analyser and more or less rewrote it from the ground up so that it can analyse and understand the dual scope (values and types) nature of TS.
So now you need to define globals for global types (as you would for global variables) so that rules can correctly understand your project.
I also get
'NodeJS' is not definedfrom no-undef, but that is defined - in @types/node. The other undefined messages, e.g. from “as const” are gone but this one remains.#2462 covers the
type-assertion-no-undefcase.Btw, after fixing a lot of instances of the no-redeclare issues in our large codebase it seems that the current behavior is beneficial. Some 80%-90% of the situations where type is named the same as function/variable are not correct and requires rename of the type. Only the remaining cases are ones where it is actually intended to simulate class/enum-like variable/type combo.
The recommended set turns of
no-undefbecause it’s covered by TS, so double reporting is somewhat useless.Some people do prefer to have the rule on so they can get the errors via a lint run. Each to their own.
parserOptions.libwill include types that are defined by typescript itself. TheJSXnamespace is actually a 3rd party namespace, defined in@types/react.You can define any additional globals you need for your project: https://eslint.org/docs/user-guide/configuring#specifying-globals