编译create-react-app项目时,TypeScript React项目不确定的错误 [英] TypeScript React project inconclusive bugs when compiling create-react-app project
问题描述
这是一个不寻常的问题,但是我经常遇到打字稿编译器(或者可能是webpack或相关的问题)。
This is a bit of an unusual question but I'm having frequent problems with the typescript compiler (or maybe webpack, or related).
这种情况每天大约发生两次(错误消息每次都不同):
This happens to me about twice a day (the error messages are different each time):
1。)我进行正常的代码更改。
2.)我收到一些奇怪的莫名其妙的错误-最新的错误是:
1.) I do normal code changes. 2.) I receive some weird inexplicable bug - the most recent was:
第0行:解析错误:无法读取属性 map未定义
Line 0: Parsing error: Cannot read property 'map' of undefined
- 很明显,除import语句外,模块中第0行没有任何代码。但是我检查了我的文件中是否有各种
.map
语句,并尝试查找错误,同时重启开发服务器两次(相同错误)。 - 最终,该错误神秘地消失了-在这种情况下,我只是注释了我所有的import语句,并在未更改任何其他内容后不久就对它们进行了注释(我没有不小心修复了该错误)。
- Obviously, there is no code at line 0 anywhere in my modules except for import statements. But I check my file for all kinds of
.map
statements and try to find the bug, restart the development server a couple of times meanwhile (same error). - Eventually, the bug mysteriously disappears - in this case, I just commented all my import statements and uncommented them shortly after without changing anything else (I did not accidentally fix the bug).
现在每天发生多次。我使用的是Typescript 4.0.2,现在我已降级为3.9.7。
This is happening multiple times a day now. I was on Typescript 4.0.2, now I've downgraded to 3.9.7.
有什么方法可以重置吗?可以解决此问题的编译器(例如,删除临时文件)?我真的不确定该怎么做。
我正在使用具有以下依赖项的create-react-app:
I'm using create-react-app with the following dependencies:
"dependencies": {
"@date-io/date-fns": "^1.3.13",
"@date-io/moment": "^1.3.13",
"@material-ui/core": "^4.11.0",
"@material-ui/icons": "^4.9.1",
"@material-ui/lab": "^4.0.0-alpha.56",
"@material-ui/pickers": "^3.2.10",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/lodash": "^4.14.157",
"@types/node": "^12.0.0",
"@types/react": "^16.9.41",
"@types/react-dom": "^16.9.0",
"@types/react-redux": "^7.1.9",
"@types/react-router-dom": "^5.1.5",
"@types/recharts": "^1.8.14",
"@types/redux-persist": "^4.3.1",
"@types/styled-components": "^5.1.0",
"@types/yup": "^0.29.3",
"axios": "^0.19.2",
"classnames": "^2.2.6",
"date-fns": "^2.15.0",
"formik": "^2.1.5",
"lodash": "^4.17.15",
"moment": "^2.27.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-intl": "^5.2.1",
"react-redux": "^7.2.1",
"react-router-dom": "^5.2.0",
"react-scripts": "^3.4.1",
"recharts": "^1.8.5",
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.8",
"redux-persist": "^6.0.0",
"redux-thunk": "^2.3.0",
"styled-components": "^5.1.1",
"yup": "^0.29.1"
},
"devDependencies": {
"@testing-library/react": "^9.5.0",
"typescript": "^3.9.7"
}
我的tsconfig:
{
"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",
"noImplicitAny": false,
"baseUrl": "src"
},
"include": [
"src"
]
}
推荐答案
问题是 react-scripts
使用的是eslint的旧版本。 关注问题
@ typescript-eslint / eslint-plugin: ^ 2.10.0
@ typescript-eslint / parser: ^ 2.10.0 ;
请注意,手动升级项目中的软件包无效
The issue is react-scripts
is using older versions of eslint. follow issue @typescript-eslint/eslint-plugin": "^2.10.0"
@typescript-eslint/parser": "^2.10.0"
Note manually upgrading packages in project didn't work
反应脚本
也给出对等依赖性错误
react-script
also gives peer dependency error
react-scripts@3.4.3需要一个对等的typescript@^3.9.2,但已安装typescript@^4.0.2。
现在我们可以降级为 typescript@^3.9.2
直到反应脚本
支持 typescript@^4.0
这篇关于编译create-react-app项目时,TypeScript React项目不确定的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!