typescript-eslint: [no-use-before-define] `import * as React` marked as error in TSX/JSX file
- 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
.eslintrc.js
{
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2018,
ecmaFeatures: {
jsx: true,
},
},
rules: {
'no-use-before-define': 'off',
'@typescript-eslint/no-use-before-define': ['error', {variables: false}],
}
}
tsconfig.json
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node",
"target": "es5",
"allowJs": true,
"importHelpers": true,
"jsx": "react",
"experimentalDecorators": true,
"isolatedModules": true,
"importsNotUsedAsValues": "error"
}
}
// foo.tsx
import * as React from 'react';
const App = () => (
<span>hello</span>
);
Expected Result
No lint errors
Actual Result
foo.tsx 1:13 React was used before it was defined. @typescript-eslint/no-use-before-define
Additional Info
- AFAICT the error was introduced in 4.0.2-alpha.12 (alpha11 is ok)
- essentially, in the project I’m involved with every .tsx / .jsx file that contains React import is affected
Versions
| package | version |
|---|---|
@typescript-eslint/eslint-plugin |
4.0.2-alpha.15 |
@typescript-eslint/parser |
4.0.2-alpha.15 |
TypeScript |
4.0.2 |
ESLint |
7.8.1 |
node |
10.16.3 |
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 4
- Comments: 15 (6 by maintainers)
Commits related to this issue
- fix(eslint-plugin): [no-use-before-define] false positive with jsx pragma reference Fixes #2502 — committed to typescript-eslint/typescript-eslint by bradzacher 6 years ago
- fix(eslint-plugin): [no-use-before-define] false positive with jsx pragma reference (#2503) Fixes #2502 — committed to typescript-eslint/typescript-eslint by bradzacher 6 years ago
- style: disable @typescript-eslint/no-use-before-define See https://github.com/typescript-eslint/typescript-eslint/issues/2502 — committed to bifravst/app by coderbyheart 6 years ago
- Fixed https://github.com/typescript-eslint/typescript-eslint/issues/2502 — committed to chungweileong94/eslint-config-whiteroom by chungweileong94 6 years ago
“eslint-plugin-react”: “^7.20.6”, “eslint”: “^7.8.1”,
error ‘React’ was used before it was defined no-use-before-define
I dont know, feel confused
I’m also still getting this error with:
"@typescript-eslint/eslint-plugin": "^4.1.0""@typescript-eslint/parser": "^4.1.0"https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/FAQ.md#i-am-using-a-rule-from-eslint-core-and-it-doesnt-work-correctly-with-typescript-code
You’re using the wrong rule
Hi confused guys, just use this in your config:
Maybe it is just our preset don’t aware of this rule yet, you can enable this new rule by hand:
'@typescript-eslint/no-use-before-define': [1],no-use-before-definehave many false alarm ↑ , while'@typescript-eslint/no-use-before-define'doesn’t ↓Along with this issue, @typescript-eslint/parser 4.0.2-alpha.12 started to trigger
no-undefat JSX intrinsic elements (i.e.<div>) in .jsx files. I have read #2477 and corresponding FAQ entry. Adding"JSX": "readonly"global didn’t help; adding each intrinsic element to globals would be very silly.@bradzacher should I file a new issue?
I figured as much - the rule does
<not<=, hence the false positive. Easy fix.