Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Types for useFormik #2924

Open
fny opened this issue Nov 19, 2020 · 14 comments
Open

Return Types for useFormik #2924

fny opened this issue Nov 19, 2020 · 14 comments

Comments

@fny
Copy link

fny commented Nov 19, 2020

We make use of useFormik to bind formik to another UI library, Everything works swimmingly. One issue though, is that there is no common return type for useFormik

// Consider
const example = useFormik<any>({ initialValues: {}, onSubmit: () =>{} })

// This is the type if example
const example: {
    initialValues: any;
    initialErrors: FormikErrors<unknown>;
    initialTouched: FormikTouched<unknown>;
    initialStatus: any;
    handleBlur: {
        (e: FocusEvent<any>): void;
        <T = any>(fieldOrEvent: T): T extends string ? (e: any) => void : void;
    };
    ... 32 more ...;
    submitCount: number;
}

It would be useful if there was a common interface for this value.

I believe it's equivalent to FormikState<any> & FormikComputedProps<any> & FormikHelpers<any> & FormikHandlers but I'm not sure.

I propose adding the following to the types file if it's actually a complete representation of useFormik's output.

type FormikInstance<Values> = FormikState<Values> & FormikComputedProps<Values> & FormikHelpers<Values> & FormikHandlers
@johnrom
Copy link
Collaborator

johnrom commented Nov 19, 2020

Agreed

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Jan 21, 2021
@johnrom johnrom removed the stale label Jan 21, 2021
@JMA12
Copy link

JMA12 commented Jan 24, 2021

I'm also looking for this one

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Feb 24, 2021
@johnrom johnrom removed the stale label Apr 26, 2021
@johnrom johnrom reopened this Apr 26, 2021
@alanszp
Copy link

alanszp commented May 24, 2021

+1 want me to make a PR? Can you give me some advice?

@johnrom
Copy link
Collaborator

johnrom commented May 24, 2021

The types for Formik in v2 aren't set up to easily generate a complete return type. useFormik has a complete return type in my v3 branch. You can check it out here

This consists of:

FormikApi & FormikConfig

Which ultimately breaks down to

type FormikApiAndConfig = 
    // API
    FormikHelpers & 
    FormikHandlers & 
    FormikRegistration & 
    FormikFieldHelpers & 
    { getValueFromEvent } &  
    // Config
    FormikValidationConfig<Values> & 
    { enableReinitialize: boolean } &
    FormikState; // replaced with FormikStateHelpers in v3

Some of those types are newly created in my v3 branch so you'd have to dig deep to recreate them for v2.

In v3 these types are spread across two contexts, FormikContext (FormikApi) and FormikConfigContext (FormikConfig).

@alanszp
Copy link

alanszp commented May 25, 2021

Making it:

Base on your types:

All this comments are based on v2 code

type FormikApiAndConfig = 
    // API
    FormikHelpers &  --> same
    FormikHandlers &  ---> same
    FormikRegistration &  --> same 
    FormikFieldHelpers &  --> didn't find in your commit. Used FormikComputedProps 
    { getValueFromEvent } &  --> not returned by useFormik v2
    // Config
    FormikValidationConfig<Values> &  --> didn't exist; extracted as a type from FormikSharedConfig
    { enableReinitialize: boolean } & --> not returned by useFormik v2
    FormikState; // replaced with FormikStateHelpers in v3 --> same

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Jun 25, 2021
@johnrom johnrom removed the stale label Jun 25, 2021
@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Jul 26, 2021
@johnrom johnrom removed the stale label Jul 26, 2021
@basile-savouret
Copy link

Hello @fny , @johnrom.
I did export type Formik = ReturnType<typeof useFormik> isn't this what you were looking for?

@johnrom
Copy link
Collaborator

johnrom commented Aug 18, 2021

@basile-savouret typeof automatically removes generics. These two are not the same:

const formikVal: ReturnType<typeof useFormik> = useFormik({ hello: string });
const typedFormikVal = useFormik({ hello: string });

One is FormikApi<any> and the other is FormikApi<{ hello: string }>. Even if that wasn't the case, there is the additional issue that not every value returned from useFormik is well-defined, but many are inferred by typescript. Defining the return type in TypeScript solves this issue as well.

@basile-savouret
Copy link

It's true. It works out for me because i don't need the generics handle by formik.

@johnrom
Copy link
Collaborator

johnrom commented Aug 18, 2021

The generic helps to prevent "wild guesses" when it comes to Field names and such, especially in the typed v3.1 PR #3152 which you can test here:

https://www.npmjs.com/package/@johnrom/formik-v3/v/3.1.0-types12

if you're not using completely dynamic data, I'd recommend trying it out as it can help discover bugs that haven't been discovered yet. I would locate type issues using that package and then push to production using the Formik production version you're already using.

Both that PR and the v3.0 PR #3231 have a strongly typed return value already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants