gpt4 book ai didi

typescript - 在 onPress 中 React Native Typescript Formik 的 handleSubmit 类型

转载 作者:行者123 更新时间:2023-12-03 19:19:53 29 4
gpt4 key购买 nike

我刚刚使用 Typescript 在 React Native 上启动了一个项目,并使用 Formik 来在我的应用程序中构建表单。

我做得很好 formik tutorial guide并看到了 formik with RN section in JS 的使用.

然而,当我试图将这个例子翻译成 typescript 时,我在 Button's onPress attribute 上遇到了以下打字错误:

No overload matches this call.
Overload 1 of 2, '(props: Readonly<ButtonProps>): Button', gave the following error.
Type '(e?: FormEvent<HTMLFormElement>) => void' is not assignable to type '(ev: NativeSyntheticEvent<NativeTouchEvent>) => void'.
Types of parameters 'e' and 'ev' are incompatible.
Type 'NativeSyntheticEvent<NativeTouchEvent>' is not assignable to type 'FormEvent<HTMLFormElement>'.
Types of property 'nativeEvent' are incompatible.
Type 'NativeTouchEvent' is missing the following properties from type 'Event': bubbles, cancelBubble, cancelable, composed, and 17 more.
Overload 2 of 2, '(props: ButtonProps, context?: any): Button', gave the following error.
Type '(e?: FormEvent<HTMLFormElement>) => void' is not assignable to type '(ev: NativeSyntheticEvent<NativeTouchEvent>) => void'.ts(2769)
index.d.ts(6926, 5): The expected type comes from property 'onPress' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<Button> & Readonly<ButtonProps> & Readonly<{ children?: ReactNode; }>'
index.d.ts(6926, 5): The expected type comes from property 'onPress' which is declared here on type 'IntrinsicAt...


据我了解, handleSubmit功能 (e?: FormEvent<HTMLFormElement>) => void , 接受来自表单提交的事件( onSubmit 元素的 form 属性)。由于在 RN 中没有等效的 Form,它提示收到 (ev: NativeSyntheticEvent<NativeTouchEvent>) => void来自 RN Button's onPress属性。

为了摆脱错误并继续前进,我使用了以下我不满意的解决方法:
 <Button
title="Do it !"
color={colors.red}
onPress={
(handleSubmit as unknown) as (
ev: NativeSyntheticEvent<NativeTouchEvent>
) => void
}
/>

我想知道我应该如何正确解决这个打字错误。

CodeSandbox: App.tsx:32

感谢您的帮助。

最佳答案

你可以这样描述你的类型:(event: GestureResponderEvent) => void;
Code sample

关于typescript - 在 onPress 中 React Native Typescript Formik 的 handleSubmit 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59599085/

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