npm 原生React:“Component”不能用作JSX组件,类型“{}”不能分配给类型“ReactNode”

u2nhd7ah  于 2022-11-14  发布在  React
关注(0)|答案(4)|浏览(277)

我刚刚开始了一个新的项目,但我得到了以下各种包的错误,例如React原生Reanimated和React导航堆栈。
这是我的package.json。

{
  "name": "foodmatch",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "expo start --dev-client",
    "android": "expo run:android",
    "ios": "expo run:ios",
    "web": "expo start --web",
    "prepare": "yarn husky install",
    "lint": "yarn eslint --ext .tsx --ext .ts src/ --fix",
    "format": "yarn prettier --write ./src"
  },
  "lint-staged": {
    "src/**/*.{ts,tsx}": [
      "eslint --ext .tsx --ext .ts src/ --fix"
    ],
    "./src/**": [
      "prettier --write ./src"
    ]
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "dependencies": {
    "@react-navigation/native": "^6.0.10",
    "@react-navigation/stack": "^6.2.1",
    "expo": "~44.0.2",
    "expo-app-loading": "~1.3.0",
    "expo-font": "^10.0.5",
    "expo-splash-screen": "~0.14.1",
    "expo-status-bar": "~1.2.0",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
    "react-native-gesture-handler": "~2.1.0",
    "react-native-reanimated": "~2.3.1",
    "react-native-responsive-fontsize": "^0.5.1",
    "react-native-safe-area-context": "3.3.2",
    "react-native-web": "0.17.1",
    "styled-components": "^5.3.5"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@types/react": "~17.0.21",
    "@types/react-native": "~0.64.12",
    "@types/styled-components-react-native": "^5.1.3",
    "@typescript-eslint/eslint-plugin": "^5.20.0",
    "@typescript-eslint/parser": "^5.20.0",
    "babel-plugin-module-resolver": "^4.1.0",
    "eslint": "^8.13.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-jsx-a11y": "^6.5.1",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-react": "^7.29.4",
    "eslint-plugin-react-hooks": "^4.4.0",
    "husky": "^7.0.4",
    "lint-staged": "^12.3.8",
    "prettier": "^2.6.2",
    "typescript": "~4.3.5"
  },
  "private": true
}

这就是错误。
第一个
我有一个具有相同包和配置(babel、eslint等)的项目,它工作正常,它看起来像一个类型错误,因为该项目的工作方式就像它的目的一样,但IntelliSense声明这些错误。
当我尝试从styled-components中使用一个带有Styled()的组件时,我也会得到以下结果。

No overload matches this call.
  Overload 1 of 2, '(component: AnyStyledComponent): ThemedStyledFunction<any, DefaultTheme, any, any>', gave the following error.
    Argument of type 'typeof Text' is not assignable to parameter of type 'AnyStyledComponent'.
      Type 'typeof Text' is not assignable to type 'StyledComponent<any, any, any, never>'.
        Type 'typeof Text' is not assignable to type 'string'.
  Overload 2 of 2, '(component: ComponentType<any>): ThemedStyledFunction<ComponentType<any>, DefaultTheme, {}, never>', gave the following error.
    Argument of type 'typeof Text' is not assignable to parameter of type 'ComponentType<any>'.
      Type 'typeof Text' is not assignable to type 'ComponentClass<any, any>'.
        Types of property 'contextType' are incompatible.
          Type 'React.Context<any> | undefined' is not assignable to type 'import("D:/Users/bilug/Documents/Repositorios/foodMatch/node_modules/@types/react-native/node_modules/@types/react/index").Context<any> | undefined'.
            Type 'React.Context<any>' is not assignable to type 'import("D:/Users/bilug/Documents/Repositorios/foodMatch/node_modules/@types/react-native/node_modules/@types/react/index").Context<any>'.
              The types of 'Provider.propTypes' are incompatible between these types.
                Type 'React.WeakValidationMap<React.ProviderProps<any>> | undefined' is not assignable to type 'import("D:/Users/bilug/Documents/Repositorios/foodMatch/node_modules/@types/react-native/node_modules/@types/react/index").WeakValidationMap<import("D:/Users/bilug/Documents/Repositorios/foodMatch/node_modules/@types/react-native/node_modules/@types/react/index").ProviderProps<any>> | undefined'.
                  Type 'React.WeakValidationMap<React.ProviderProps<any>>' is not assignable to type 'import("D:/Users/bilug/Documents/Repositorios/foodMatch/node_modules/@types/react-native/node_modules/@types/react/index").WeakValidationMap<import("D:/Users/bilug/Documents/Repositorios/foodMatch/node_modules/@types/react-native/node_modules/@types/react/index").ProviderProps<any>>'.
                    Types of property 'children' are incompatible.
                      Type 'React.Validator<React.ReactNode> | undefined' is not assignable to type 'import("D:/Users/bilug/Documents/Repositorios/foodMatch/node_modules/@types/react-native/node_modules/@types/react/index").Validator<import("D:/Users/bilug/Documents/Repositorios/foodMatch/node_modules/@types/react-native/node_modules/@types/react/index").ReactNode> | undefined'.
                        Type 'React.Validator<React.ReactNode>' is not assignable to type 'import("D:/Users/bilug/Documents/Repositorios/foodMatch/node_modules/@types/prop-types/index").Validator<import("D:/Users/bilug/Documents/Repositorios/foodMatch/node_modules/@types/react-native/node_modules/@types/react/index").ReactNode>'.
                          Type 'React.ReactNode' is not assignable to type 'import("D:/Users/bilug/Documents/Repositorios/foodMatch/node_modules/@types/react-native/node_modules/@types/react/index").ReactNode'.
                            Type '{}' is not assignable to type 'ReactNode'.ts(2769)
raogr8fs

raogr8fs1#

@types/react添加到package.json中的resolutions中修复了该问题:

"resolutions": { "@types/react": "17.0.43" }
rhfm7lfc

rhfm7lfc2#

我添加了以下依赖项,它解决了问题:

"@types/react-dom": "17.0.14",
"@types/react": "~18.0.0",
"@types/react-native": "~0.69.1",

https://github.com/eslint/eslint/issues/15802#issuecomment-1205018068

6ljaweal

6ljaweal3#

“决议”:{“@类型/React”:“17.0.43”},

将此添加到package.json

删除(节点模块)并重新安装纱或npm

yiytaume

yiytaume4#

感谢@礼萨巴巴伊。
对我来说,修复是,不是在resolutions中。我的问题是:
我的"@types/react"是在"~17.0.21",
而我的"react":"17.0.1",

他们需要匹配

当我把我的"@types/react"设置为在"~17.0.1",匹配reaction并运行install时。它立即修复了我的问题。我也可以只更新两者。其他的解决方案都不适合我

相关问题