I am working with create-react-app + typescript + eslint application and during build have such error:
Line 1:8: \'React\' was used before it was defined @t
(not enough rep to comment)
@sashko's workaround seems to work - deleting node_modules
was necessary as well.
One thing I missed was the caret in "^4.0.1"
. The version needs to be fixed without the caret ^
, which seems to be the issue that @Rolly is having. Make sure it's 4.0.1
.
Only a workaround until react-scripts
updates to v4 (I'm using create-react-app
)
Try using Yarn instead of NPM (make sure to remove your node_modules
in between).
This worked for me.
From the official documentation.
"rules": {
// note you must disable the base rule as it can report incorrect errors
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": ["error"]
}
The bug occurs due to mismatch of @typescript-eslint
versions in react-scripts and your local package.json
- GitHub issue
You can downgrade the packages until react-scripts
updates their version.
"@typescript-eslint/eslint-plugin": "4.0.1",
"@typescript-eslint/parser": "4.0.1",
EDIT 14/09/2020
It seems the bug is not related to react-scripts
version of @typescript-eslint
since multiple people reported the same bug without using react-scripts
.
Anyway, the workaround remains the same - downgrade to the working version of @typescript-eslint
until the fix is available.
EDIT 24/10/2020
react-scripts@4.0.0
has been published with updated @typescript-eslint
. Using the newest version should solve the issue.
EDIT 04/11/2020
If after upgrading the packages the error is still there, most probably your eslint config uses the wrong rule. Check out Igor's answer to fix it.
That is how I resolved my problem.
cache: true, // You can set it to false
formatter: require.resolve('react-dev-utils/eslintFormatter'),
EXTEND_ESLINT=true
rules: {
"@typescript-eslint/no-use-before-define": "off"
},
Try adding import/resolver to your Eslint
settings:
"settings": {
"react": { "version": "detect" },
"import/resolver": { "typescript": {} }
}
Maybe you will need to install it too:
$ yarn add -D eslint-import-resolver-typescript
If that doesn't work, you could change this rule
"@typescript-eslint/no-use-before-define": "off"
like this:
"no-use-before-define": "off"