gpt4 book ai didi

reactjs - 为什么 TypeScript 告诉我 NativeBase 的 Button Component 上不存在某个属性,但仍然呈现它?

转载 作者:行者123 更新时间:2023-12-05 05:50:38 27 4
gpt4 key购买 nike

我在 React Native 项目上使用 NativeBase,虽然当我使用 Xcode 在我的 iPhone 上运行应用程序时一切正常,但 TypeScript 告诉我属性“onPress”在 NativeBase 按钮​​的类型上不存在。

为了简化这里是我的代码:

import { Button } from 'native-base';

...

<Button
variant="ghost"
colorScheme="blueGray"
onPress={() => {
console.log('Ok')
}}
>

我得到的错误:

Type '{ children: string; onPress: () => void; }' is not assignable to type 'IntrinsicAttributes & IButtonProps & { ref?: MutableRefObject | undefined; }'.Property 'onPress' does not exist on type 'IntrinsicAttributes & IButtonProps & { ref?: MutableRefObject | undefined; }'.ts(2322)

同样,代码工作正常,但在我的项目的每个文件中每个按钮都会出现一个错误,这很烦人。

我不太习惯 React Native 和 NativeBase,所以我找不到它抛出此错误的原因。

我试着查看一下文件,看看是否可以在某处找到“onPress”属性,因为它正在工作并且在文档中,我发现根据 TS :

import type { PressableProps } from 'react-native';

Module '"react-native"' has no exported member 'PressableProps'.ts(2305)

我的问题是为什么 TS 告诉我 onPress 不存在,但我认为它可以以某种方式提供帮助,因为它看起来应该是 onPress 所在的位置。

最佳答案

在创建全新安装后,我发现出于某种原因 - 可能是因为我进行了更新以及我现在没有使用很多组件 - 而 React-Native 的版本是 0.64.X , 类型在版本 0.62.X 上。

所以解决方案是更改package.json中@types/react-native的版本

TypeScript 不知道 onPress 属性的存在,但它仍然存在,所以我在运行应用程序时没有遇到任何问题。由于我将类型更新为正确的版本,它不再告诉我该属性不存在。

关于reactjs - 为什么 TypeScript 告诉我 NativeBase 的 Button Component 上不存在某个属性,但仍然呈现它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70498090/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com