在我所有的(() => {
const canvas = document.currentScript.closest('section').querySelector('canvas');
// ...
})();
或.ts
文件中,我在文件的第一个字符上都显示了此警告,如下图所示:
我正在使用标准的CRA设置。这是我的.tsx
:
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"
]
}
tsconfig.json
知道我缺少什么吗?
答案 0 :(得分:37)
当我看到同样的错误时,我最近更新了我对项目的依赖。简单地重新启动 Visual Code 似乎消除了错误。
答案 1 :(得分:1)
更新依赖项时也要检查您的 resolutions
。由于 this issue 修复了 ts eslint 版本,我在将 react-scripts
更新到 v4 时得到了这个:
"resolutions": {
"@typescript-eslint/eslint-plugin": "2.23.0",
"@typescript-eslint/parser": "2.23.0",
"@typescript-eslint/typescript-estree": "2.23.0"
},
答案 2 :(得分:0)
我有同样的问题。我修复它的方法是(a)更新解析器,并(b)创建使用此最新版本的文件eslintrc.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",
eslintrc.json就像:
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"]
}
};
答案 3 :(得分:0)
我遇到了同样的问题,我通过将以下规则添加到我在 typescript-eslint 存储库中找到的 Eslint 配置中来修复它。
{
// note you must disable the base rule as it can report incorrect errors
"no-redeclare": "off",
"@typescript-eslint/no-redeclare": ["error"]
}
<块引用>
一定要重启打字稿服务器!
答案 4 :(得分:0)
当我看到同样的错误时,我最近更新了我对项目的依赖。简单地重新启动 Visual Code 似乎消除了错误。
我尝试了此处提供的其他各种解决方案,但都没有奏效。由于我一直在更新软件包 (yarn upgrade-interactive --latest
),我想我的 node_modules
可能不是理想的。在我清除缓存并重新安装 node_modules
后,错误消失了。
npx lerna clean -y && yarn cache clean && yarn install
或者,如果您不在 lerna(单声道)存储库中,请执行以下操作:
rm -rf node_modules && yarn cache clean && yarn install
由于错误只出现在 vscode 中(而不是在运行 lint 时出现),我重新启动了 vscode,错误消失了。
答案 5 :(得分:0)
我正在使用具有 Yarn 工作区的项目。事实证明,两个项目使用了 eslint-config-react-app
,一个使用的是没有此规则的旧版本。不幸的是,这是正在运行的版本。
要使用 Yarn 进行故障排除,请使用 yarn why eslint-config-react-app
。这将告诉您安装包的原因和版本。
我通过在顶级 package.json 文件中添加 Yarn resolution 解决了我的问题:
"resolutions": {
"eslint-config-react-app": "6.0.0"
},
(您应该将版本固定到 CRA 使用的任何版本)。
答案 6 :(得分:0)
如果没有什么对你有用,对我来说,这可能对你也有帮助。
如果您使用 create-react-app,请在 package.json
中添加此“规则” "eslintConfig": {
"extends": "react-app",
"rules": {
"@typescript-eslint/no-redeclare": "off",
"no-redeclare": "off"
}
}