在 onPress 中对 handleSubmit 的 React Native Typescript Formik 打字

React Native Typescript Formik typings of handleSubmit in onPress

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

我做得很好formik tutorial guide and saw the use of formik with RN section in JS

但是,当我尝试将此示例翻译成打字稿时,我在 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 接受来自表单提交的事件(form 元素的 onSubmit 属性)。由于 RN 中没有等效的表单,它抱怨从 RN Button's onPress 属性接收到 (ev: NativeSyntheticEvent<NativeTouchEvent>) => void

为了消除错误并继续前进,我使用了以下我不满意的解决方法:

 <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