React、Typescript 和 Material-UI 的编译问题 4

Compilation issue with React, Typescript and Material-UI 4

突然之间,我的整个构建都崩溃了,无法构建。几天前我用一个新的 create-react 应用程序构建重置了项目,它有一段时间没问题,然后昨天 - 类似的问题有不同的错误:

Failed to compile.

TS2322: Type '{ children: Element; xsDown: true; }' is not assignable to type 'IntrinsicAttributes & HiddenProps'.
  Property 'children' does not exist on type 'IntrinsicAttributes & HiddenProps'.
    208 |                     {studentName(x)} <{x.student}>
    209 |                 </Box>
  > 210 |                 {!locked && <Hidden xsDown={true}><Box><RemoveButton deleteClickHandler={deleteClickHandler} x={x}/></Box></Hidden>}
        |                              ^^^^^^
    211 |                 {locked &&
    212 |                 <Box>
    213 |                     <Tooltip title="Locked"><Lock/></Tooltip>


error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我已经删除了一些我在小地方使用的库,我怀疑它们会导致类型声明冲突,但是,我仍然遇到这个问题,在这一点上,我完全被困住了。

Package.json 不是很复杂:

  "name": "****Redacted****",
  "version": "1.2.0",
  "private": true,
  "dependencies": {
    "@date-io/date-fns": "^1.3.13",
    "@fortawesome/fontawesome-common-types": "^6.1.1",
    "@fortawesome/fontawesome-svg-core": "^6.1.1",
    "@fortawesome/free-brands-svg-icons": "^6.1.1",
    "@fortawesome/free-solid-svg-icons": "^6.1.1",
    "@fortawesome/react-fontawesome": "^0.1.18",
    "@material-table/core": "^4.3.39",
    "@material-ui/core": "^4.12.3",
    "@material-ui/icons": "^4.11.3",
    "@sentry/react": "^6.19.6",
    "@sentry/tracing": "^6.19.6",
    "@stripe/react-stripe-js": "^1.7.0",
    "@stripe/stripe-js": "^1.26.0",
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^12.0.0",
    "@testing-library/user-event": "^13.2.1",
    "@types/dateformat": "^5.0.0",
    "@types/jest": "^27.0.1",
    "@types/react": "^18.0.0",
    "@types/react-dom": "^18.0.0",
    "@types/react-router-dom": "^5.3.3",
    "amazon-cognito-identity-js": "^5.2.8",
    "amazon-cognito-identity-js-typescript": "^1.22.0",
    "axios": "^0.26.1",
    "dateformat": "^4.6.3",
    "immutability-helper": "^3.1.1",
    "react": "^18.0.0",
    "react-dnd": "^14.0.5",
    "react-dnd-html5-backend": "14.1.0",
    "react-dnd-touch-backend": "14.1.0",
    "react-dom": "^18.0.0",
    "react-router": "^6.3.0",
    "react-router-dom": "^6.3.0",
    "react-scripts": "5.0.0",
    "sass": "^1.49.11",
    "typescript": "^4.6.3",
    "web-vitals": "^2.1.0"
  },
  "scripts": {
    "start": "GENERATE_SOURCEMAP=false react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {}
}

我相信这是因为您已经更新到 React 18(具体来说,"@types/react": "^18.0.0",)。

This pull request 谈论发生了什么变化。

因为问题源于 Material Ui 4 组件,您很可能需要执行以下操作之一:

  1. 升级到 Mui5
  2. 降级到 React 17
  3. 等待 this PR 合并,这似乎可以缓解您遇到的问题。

(此外,来自 MUI4 does implementchildren?: React.ReactNode; 儿童 children?: React.ReactNode; 就像第一个 link 提到的一样)