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

FieldArray, Binding element 'form' implicitly has an 'any' type. #3885

Open
Tomekmularczyk opened this issue Sep 14, 2023 · 1 comment
Open

Comments

@Tomekmularczyk
Copy link

Bug report

Current Behavior

Since [email protected] I cannot update it because typings of FieldArray break

image

image

Expected behavior

FieldArray should be correctly typed

Your environment

Software Version(s)
Formik 2.4.4
React 18.2.0
TypeScript 5.2.2
Browser chrome
npm/Yarn 1.22.17
Operating System macos
@Zikoat
Copy link

Zikoat commented Dec 12, 2023

The same case is for normal Field.
Related: #2086, it was closed, but people seem to still have the issue as it was never fixed.

Workaround:

  const initialValues = {
    firstName: '',
    lastName: '',
    email: '',
  };

  return (
    <Formik initialValues={initialValues} onSubmit={() => {}}>
      <Form>
        <Field name="lastName">
          {({ field, form, meta }: FieldProps<typeof initialValues['lastName']>) => {
            field.value; 
            // field.value now has type string
            return (
              <div>
                <input type="text" placeholder="Last name" {...field} />
                {meta.touched && meta.error && <div className="error">{meta.error}</div>}
              </div>
            );
          }}
        </Field>
      </Form>
    </Formik>
  );

Notice the added : FieldProps<typeof initialValues['lastName']>, which tells typescript what type the props should have.

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

Successfully merging a pull request may close this issue.

2 participants