没有重载与此调用匹配。重载 2 个中的 1 个“(props: ViewProps | Readonly<ViewProps>): View”,出现以下错误。 (反应本机)

问题描述 投票:0回答:3
(alias) class View
import View
No overload matches this call.
  Overload 1 of 2, '(props: ViewProps | Readonly<ViewProps>): View', gave the following error.
    Type '{ children: Element[]; style: { flexDirection: "row"; alignItems: "center"; marginTop: number; }; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<ViewProps>'.
      Property 'children' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<ViewProps>'.
  Overload 2 of 2, '(props: ViewProps, context: any): View', gave the following error.
    Type '{ children: Element[]; style: { flexDirection: "row"; alignItems: "center"; marginTop: number; }; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<ViewProps>'.
      Property 'children' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<ViewProps>'.ts(2769)

我不知道为什么会出现此错误。 甚至回顾了其他堆栈溢出类似的问题。问题没有解决。

我正在使用从 React Native 导入的样式表。

代码:-

       <View style={styles.createPostContainer}>
        <View style={styles.addPostButtonContainer}>
          <TouchableOpacity
            data-elementId="button_next"
            onPress={this.discordChanges}
          >
            <Image
              data-elementId="image_Back"
              style={[DS ? styles.image_BackImage_5inch : styles.image_BackImage]}
              source={back}
            />
          </TouchableOpacity>
        </View>
        <Text
          data-elementId="textlabel_Payments"
          style={[DS ? styles.textlabel_PaymentsText_5inch : styles.textlabel_PaymentsText]}
        >
          Create Post
        </Text>
      </View>

我的项目信息:-

npx react-native info
info Fetching system and libraries information...
System:
    OS: macOS 13.0.1
    CPU: (8) arm64 Apple M1
    Memory: 199.59 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.9.1 - /opt/homebrew/bin/node
    Yarn: 1.22.18 - /opt/homebrew/bin/yarn
    npm: 8.19.1 - /opt/homebrew/bin/npm
    Watchman: 2022.09.19.00 - /opt/homebrew/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 16.0, macOS 12.3, tvOS 16.0, watchOS 9.0
  IDEs:
    Android Studio: 2021.3 AI-213.7172.25.2113.9014738
    Xcode: 14.0/14A309 - /usr/bin/xcodebuild
  npmPackages:
    @react-native-community/cli: ^9.1.1 => 9.1.3 
    react-native: file:./packages/blocks/core/node_modules/react-native => 0.61.3 
  npmGlobalPackages:
    react-native: 0.70.1

我尝试降低打字稿版本。不知道为什么我这样做,只是尝试了一下。 希望任何人都可以帮助我解决这个问题。 :)

javascript typescript react-native
3个回答
2
投票

将“解决方案”字段添加到您的 package.json 文件中,您可以为冲突或不兼容的包指定特定版本。通过这样做,您可以确保在项目中使用正确版本的 @types/react 和 @types/react-dom,这可能会解决您遇到的类型冲突。

以下是将“解决方案”字段添加到 package.json 的方法:

"resolutions": {
  "@types/react": "^17.0.38",
  "@types/react-dom": "^17.0.11"
}

1
投票

yarn.lock
文件中添加此代码,然后删除所有节点模块并重新安装所有依赖项。

“@types/react@”:版本“17.0.38”已解决 “https://registry.yarnpkg.com/@types/react/-/react-17.0.38.tgz#f24249fefd89357d5fa71f739a686b8d7c7202bd” 正直 sha512-SI92X1IA+FMnP3qM5m4QReluXzhcmovhZnLNm3pyeQlooi02qI7sLiepEYqT678uNiyc25XfCqxREFpy3W7YhQ== 依赖项:“@types/prop-types”“”“@types/scheduler”“*”csstype “^3.0.2”

这个解决方案是我的同事向我推荐的。另请确保删除现有的

@types/react@
,然后将其替换为我上面的解决方案。请让我知道该解决方案是否适合您。谢谢大家的帮助。


0
投票

我也遇到这个问题,花了我一天的时间来处理。我也尝试过此页面中的方法,但遗憾的是,对我不起作用。 最后,我在 package.json 中的

resolutions
配置部分添加以下行,并且对我来说效果很好。

"@types/react": "17.0.24"

resolutions

文件中的整个
package.json
配置看起来像这样:

"resolutions": { "@types/react": "17.0.24" }
希望它也能帮助其他人。

© www.soinside.com 2019 - 2024. All rights reserved.