formik icon indicating copy to clipboard operation
formik copied to clipboard

Enhancer type for setFieldTouched, setFieldValue, setFieldError

Open nguyen-ba-luan opened this issue 2 years ago • 1 comments

Hi! 👋

Here is the diff that solved my problem:

diff --git a/node_modules/formik/dist/Formik.d.ts b/node_modules/formik/dist/Formik.d.ts
index 8657eff..968247b 100644
--- a/node_modules/formik/dist/Formik.d.ts
+++ b/node_modules/formik/dist/Formik.d.ts
@@ -18,9 +18,9 @@ export declare function useFormik<Values extends FormikValues = FormikValues>({
     resetForm: (nextState?: Partial<FormikState<Values>> | undefined) => void;
     setErrors: (errors: FormikErrors<Values>) => void;
     setFormikState: (stateOrCb: FormikState<Values> | ((state: FormikState<Values>) => FormikState<Values>)) => void;
-    setFieldTouched: (field: string, touched?: boolean, shouldValidate?: boolean | undefined) => Promise<FormikErrors<Values>> | Promise<void>;
-    setFieldValue: (field: string, value: any, shouldValidate?: boolean | undefined) => Promise<FormikErrors<Values>> | Promise<void>;
-    setFieldError: (field: string, value: string | undefined) => void;
+    setFieldTouched: <T>(field: keyof Values, touched?: boolean, shouldValidate?: boolean | undefined) => Promise<FormikErrors<Values>> | Promise<void>;
+    setFieldValue: <T>(field: keyof Values, value: any, shouldValidate?: boolean | undefined) => Promise<FormikErrors<Values>> | Promise<void>;
+    setFieldError: <T>(field: keyof Values, value: string | undefined) => void;
     setStatus: (status: any) => void;
     setSubmitting: (isSubmitting: boolean) => void;
     setTouched: (touched: FormikTouched<Values>, shouldValidate?: boolean | undefined) => Promise<FormikErrors<Values>> | Promise<void>;

nguyen-ba-luan avatar Oct 16 '23 07:10 nguyen-ba-luan

I see that using keyof Values could be useful to avoid field typos at times when entering field names manually. But what's the use of the T generic?

CodeSmith32 avatar Aug 19 '24 14:08 CodeSmith32