【问题标题】:TypeScript React project inconclusive bugs when compiling create-react-app project编译 create-react-app 项目时 TypeScript React 项目不确定的错误
【发布时间】:2020-08-27 12:13:10
【问题描述】:

这是一个不寻常的问题,但我经常遇到 typescript 编译器(或者可能是 webpack 或相关)的问题。

这发生在我身上大约一天两次(每次的错误信息都不一样):

1.) 我进行正常的代码更改。 2.) 我收到了一些奇怪的莫名其妙的错误 - 最近的是:

第 0 行:解析错误:无法读取未定义的属性“地图”

  1. 显然,除了 import 语句之外,我的模块中的任何地方的第 0 行都没有代码。但是我检查了我的文件中的各种.map 语句并尝试找到错误,同时重新启动开发服务器几次(同样的错误)。
  2. 最终,该错误神秘地消失了 - 在这种情况下,我只是注释了我的所有导入语句并在没有更改任何其他内容后不久取消了它们的注释(我没有意外修复该错误)。

现在每天都会发生多次。我使用的是 Typescript 4.0.2,现在我已经降级到 3.9.7。

有没有办法“重置”编译器(例如删除临时文件)来解决这个问题?我真的不知道该怎么做。

我正在使用具有以下依赖项的 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"
  }

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

【问题讨论】:

    标签: reactjs typescript create-react-app


    【解决方案1】:

    问题是react-scripts 使用的是旧版本的 eslint。 follow issue

    @typescript-eslint/eslint-plugin": "^2.10.0"
    @typescript-eslint/parser": "^2.10.0"

    注意在项目中手动升级包不起作用

    react-script 也会给出对等依赖错误

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

    现在我们可以降级到typescript@^3.9.2,直到react-scripts支持typescript@^4.0

    【讨论】:

    • 对于那些偶然发现这个答案的人来说,它大多是正确的。暂时更新你的 package.json 将typescript 固定为^3,然后删除node_modulespackage-lock.json。之后,npm i 重新安装节点模块。随后运行npm run start 应该可以工作。如果您对最终的 typescript 4 支持感兴趣,请在此处订阅链接的 GitHub 问题以获取更新。
    猜你喜欢
    • 1970-01-01
    • 2017-08-06
    • 2021-12-02
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多