refactor: strong type for getFormHelpers name (#1029)

This commit is contained in:
G r e y
2022-04-15 16:31:23 -04:00
committed by GitHub
parent 104a3c6b9c
commit cf8a20d6f6

View File

@ -17,8 +17,13 @@ interface FormHelpers {
helperText?: string
}
export const getFormHelpers = <T>(form: FormikContextType<T>, name: string, error?: string): FormHelpers => {
// getIn is a util function from Formik that gets at any depth of nesting, and is necessary for the types to work
export const getFormHelpers = <T>(form: FormikContextType<T>, name: keyof T, error?: string): FormHelpers => {
if (typeof name !== "string") {
throw new Error(`name must be type of string, instead received '${typeof name}'`)
}
// getIn is a util function from Formik that gets at any depth of nesting
// and is necessary for the types to work
const touched = getIn(form.touched, name)
const errors = error ?? getIn(form.errors, name)
return {