React Native Typescript Formik cast event type

Viewed 1161

In the Formik documentation for React Native it has an example form:

<Formik initialValues={{ email: '' }} onSubmit={(values) => console.log(values)}>
  {({ handleChange, handleBlur, handleSubmit, values }) => (
    <View>
      <TextInput onChangeText={handleChange('email')} onBlur={handleBlur('email')} value={values.email} />
      <Button onPress={handleSubmit} title="Submit" />
    </View>
  )}
</Formik>

This, however, gives me a Typescript error:

No overload matches this call.
Overload 1 of 2, '(props: Readonly<ButtonProps>): Button', gave the following error.
  Type '(e?: FormEvent<HTMLFormElement> | undefined) => 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>'.

Given that I am getting the handleSubmit function from destructuring, how can I cast the type of event correctly?

Note: I know I can do this, but I have read that this will cause additional renders in React:

<Button onPress={(e) => handleSubmit(e as any)} title="Submit" />
1 Answers

Maybe it's not the best but you can do something like this

  <Button
     onPress={handleSubmit as (values: any) => void} // <= add this
     title="Submit" />
Related