Reputation: 6795
In all my .ts
or .tsx
files I have this warning on the first character in the file like in the screenshot below:
I'm using a standard CRA setup. Here's my package.json
:
{
"name": "my-project",
"version": "0.1.0",
"private": true,
"dependencies": {
"firebase": "^8.0.1",
"formik": "^2.2.3",
"i18next": "^19.8.3",
"i18next-browser-languagedetector": "^6.0.1",
"i18next-xhr-backend": "^3.2.2",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-firebaseui": "^4.1.0",
"react-i18next": "^11.7.3",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.0",
"react-select": "^3.1.0",
"react-spinners": "^0.9.0",
"react-syntax-highlighter": "^15.3.0",
"react-transition-group": "^4.4.1",
"styled-components": "^5.2.1",
"yup": "^0.29.3"
},
"devDependencies": {
"@cypress/webpack-preprocessor": "^5.4.10",
"@types/firebase": "^3.2.1",
"@types/jest": "^26.0.15",
"@types/react": "^16.9.56",
"@types/react-dom": "^16.9.9",
"@types/react-router-dom": "^5.1.6",
"@types/react-select": "^3.0.23",
"@types/react-syntax-highlighter": "^13.5.0",
"@types/styled-components": "^5.1.4",
"@types/yup": "^0.29.9",
"cypress": "^5.5.0",
"eslint-plugin-cypress": "^2.11.2",
"i18next-parser": "^3.3.0",
"typescript": "^4.0.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"eject": "react-scripts eject",
"cypress:open": "cypress open",
"test": "react-scripts test --runInBand",
"extract": "i18next --config i18next-parser.config.js"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
and my tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"types": ["cypress"],
"noFallthroughCasesInSwitch": true
},
"include": [
"src"
]
}
Any idea what I'm missing?
Upvotes: 23
Views: 20850
Reputation: 445
Updating the major version of eslint
fixed this for me. npm install eslint@latest
.
Note that updating the major dependency version can cause other issues, so this may not be a solution for large projects.
Upvotes: 0
Reputation: 7492
I had the same issue. The way I fixed it was to (a) update the parser in package.json
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
"eslint-config-airbnb-typescript": "^12.0.0",
"eslint-config-prettier": "^6.15.0",
and (b) create the file eslintrc.cjs
:
module.exports = {
"env": {
"es2020": true,
"node": true
},
"extends": [
"react-app",
"airbnb",
"eslint:recommended",
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 11,
"sourceType": "module"
},
"plugins": [
"prettier",
"@typescript-eslint"
],
"rules": {
"prettier/prettier": ["error"],
"no-console": ["error"]
}
};
Upvotes: 5
Reputation: 519
If nothing works for you, as for me, this might be helpful for you too.
If you use create-react-app, add this "rules" in package.json
"eslintConfig": {
"extends": "react-app",
"rules": {
"@typescript-eslint/no-redeclare": "off",
"no-redeclare": "off"
}
}
Upvotes: 1
Reputation: 181
I am using a project that has Yarn workspaces. It turns out two projects were using eslint-config-react-app
and one was using an older version without this rule. Unfortunately that was the version that was running.
To troubleshoot with Yarn, use yarn why eslint-config-react-app
. This will tell you why the package was installed and what versions.
I fixed my issue by adding a Yarn resolution in the top-level package.json file:
"resolutions": {
"eslint-config-react-app": "6.0.0"
},
(You should pin the version to whatever version that CRA is using).
Upvotes: 1
Reputation: 506
I had recently updated my dependencies on my project when I saw this same error. A simple restart of Visual Code appears to have eliminated the error.
I tried the other various solutions provided here, but none worked. Since I had been updating packages (yarn upgrade-interactive --latest
), I suppose it's possible that my node_modules
was not ideal. After I cleared the cache and reinstalled node_modules
, the error went away.
npx lerna clean -y && yarn cache clean && yarn install
Or if you aren't in a lerna (mono) repo, just do:
rm -rf node_modules && yarn cache clean && yarn install
Since the errors were only appearing in vscode (and not when running lint), I restarted vscode and the errors went away.
Upvotes: 2
Reputation: 25082
When updating dependencies check your resolutions
too. I got this when updating react-scripts
to v4 while having fixed ts eslint versions due to this issue:
"resolutions": {
"@typescript-eslint/eslint-plugin": "2.23.0",
"@typescript-eslint/parser": "2.23.0",
"@typescript-eslint/typescript-estree": "2.23.0"
},
Upvotes: 3
Reputation: 718
I had recently updated my dependencies on my project when I saw this same error. A simple restart of Visual Code appears to have eliminated the error.
Upvotes: 56
Reputation: 115
I had the same issue and I fixed it by adding the following rules to Eslint config that I found on the typescript-eslint repository.
{
// note you must disable the base rule as it can report incorrect errors
"no-redeclare": "off",
"@typescript-eslint/no-redeclare": ["error"]
}
make sure to restart the typescript server!
Upvotes: 0