jaredpalmer / formik

Build forms in React, without the tears šŸ˜­
https://formik.org
Apache License 2.0
34.02k stars 2.79k forks source link

No return type for `useFormik` function #2023

Open sgronblo opened 5 years ago

sgronblo commented 5 years ago

šŸ› Bug report

Current Behavior

The return type for useFormik is defined inline.

Expected behavior

There should be a separate type defined for the return value of useFormik

Reproducible example

N/A

Suggested solution(s)

Add an explicit return type for useformik at https://github.com/jaredpalmer/formik/blob/d90149e6e75aada13a2a02f390495d268237ce04/src/Formik.tsx#L135

Additional context

Trying to add formik step by step to an application. Want to specify the type of a component property that matches whatever useFormik returns. Currently I have to specify my own FormConfig type and add whatever I need there.

Your environment

Software Version(s)
Formik formik@2.0.3
React react@16.9.0
TypeScript typescript@3.5.3
Browser Chrome Version 78.0.3904.87
npm/Yarn yarn 1.19.1
Operating System MacOS Mojave 1.14.6
WestonThayer commented 5 years ago

I have found FormikProps to be similar, but Iā€™d like a type here too for use in sub component props.

wbobeirne commented 4 years ago

+1. Currently, if you want to use the return type of useFormik in another function, you have to type it as ReturnType<typeof useFormik>. However, useFormik is a generic argument, but you cannot specify generic arguments when using typeof. For example doing ReturnType<typeof useFormik<MyCustomFormValues>> is a syntax error.) So any object you pass in there that specified generic fields will result in the following error:

Argument of type '{ initialValues: MyCustomFormValues, initial...
  ...
  Type 'FormikValues' is not assignable to type 'MyCustomFormValues'
davidroeca commented 4 years ago

This makes wrapping useFormik with a custom wrapper mentioned in #2339 challenging/tedious when it comes to type safety/correctness.

~EDIT: Looks like FormikConfig<Values> might be :ok_hand:~ EDIT 2: Looks like FormikContextType<Values> is better

stuart-clark-45 commented 1 year ago

Found a work around for this

export type Formik<T extends FormikValues> = ReturnType<typeof useFormik<T>>;