简体   繁体   English

编译 create-react-app 项目时 TypeScript React 项目不确定的错误

[英]TypeScript React project inconclusive bugs when compiling create-react-app project

This is a bit of an unusual question but I'm having frequent problems with the typescript compiler (or maybe webpack, or related).这是一个不寻常的问题,但我经常遇到打字稿编译器(或者可能是 webpack 或相关的)的问题。

This happens to me about twice a day (the error messages are different each time):这发生在我身上大约每天两次(每次的错误消息都不同):

1.) I do normal code changes. 1.) 我做正常的代码更改。 2.) I receive some weird inexplicable bug - the most recent was: 2.) 我收到了一些奇怪的莫名其妙的错误 - 最近的是:

Line 0: Parsing error: Cannot read property 'map' of undefined第 0 行:解析错误:无法读取未定义的属性“map”

  1. Obviously, there is no code at line 0 anywhere in my modules except for import statements.显然,除了 import 语句外,我的模块中的第 0 行都没有代码。 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).但是我检查我的文件中的各种.map语句并尝试找到错误,同时重新启动开发服务器几次(同样的错误)。
  2. 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).最终,该错误神秘地消失了 - 在这种情况下,我只是评论了我所有的导入语句,并在没有更改任何其他内容后不久取消了它们的注释(我没有意外修复错误)。

This is happening multiple times a day now.这种情况现在一天发生多次。 I was on Typescript 4.0.2, now I've downgraded to 3.9.7.我使用的是 Typescript 4.0.2,现在我已经降级到 3.9.7。

Are there any ways to "reset" the compiler (eg delete temp files) that may solve this?有什么方法可以“重置”编译器(例如删除临时文件)来解决这个问题? I'm really not sure how to go about this.我真的不知道该怎么做。

I'm using create-react-app with the following dependencies:我正在使用具有以下依赖项的 create-react-app:

"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"
  }

My tsconfig:我的 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"
  ]
}

The issue is react-scripts is using older versions of eslint.问题是react-scripts使用的是旧版本的 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-script也给出了对等依赖错误

react-scripts@3.4.3 requires a peer of typescript@^3.9.2 but typescript@^4.0.2 was installed.

For now we can degrade to typescript@^3.9.2 till the time react-scripts support typescript@^4.0现在我们可以降级到typescript@^3.9.2直到react-scripts支持typescript@^4.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM