question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript compile errors with many FormikHelpers when using nested fields

See original GitHub issue

🐛 Bug report

On a project that uses Typescript and nested fields, the defs are not flexible enough for many functions in FormikHelpers.

interface Values {
  some: {
    nested: string;
  };
}

const MyEnhancedForm = withFormik({
  mapPropsToValues: (): Values => ({
    some: { nested: "" }
  }),
  handleSubmit: () => {}
})(props => {
  const { setFieldValue } = props;
  return (
    <form onSubmit={props.handleSubmit}>
      <Field name="some.nested">
        {({ field }: FieldProps<string>) => {
          return (
            <input
              type="text"
              {...field}
              onChange={event =>
                setFieldValue("some.nested", event.target.value)
              }
            />
          );
        }}
      </Field>
      <button type="submit">Submit</button>

      <DisplayFormikState {...props} />
    </form>
  );
});

In the above example, the #setFieldValue() call is invalid bc the function is defined using (field: keyof Values & string, ... Obviously some.nested doesn’t exist as a key of Values, so it doesn’t compile.

Expected behavior

Nested keys should be allowed as a field key in FormikHelpers functions.

Reproducible example

https://codesandbox.io/s/typescript-formikhelpers-defs-dont-work-for-nested-values-9r5nm

Suggested solution(s)

The field key either needs to be relaxed to string or figure out some other Typescript magic to include the nested field names.

Your environment

Software Version(s)
Formik 2.0.1-rc.12
React 16.8.6
TypeScript 3.5.1
Browser Chrome 75.0.3770.142 (latest)
npm/Yarn npm 5.6.0
Operating System MacOS 10.14.5

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:9
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
davidroecacommented, Jan 22, 2020

Still relevant

1reaction
davidroecacommented, Nov 18, 2019

This is still an issue for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to dynamically access nested errors/touched on formik ...
Formik has a function getIn() that can extract a value from an object by a path (e.g. a path being something like name.first...
Read more >
useField() - Formik
useField is a custom React hook that will automagically help you hook up inputs to Formik. You can and should use it to...
Read more >
Typescript FormikHelpers defs don't work for nested values
Typescript FormikHelpers defs don't work for nested values ... defs don't work for nested values. Template for reproducing Formik bugs using withFormik.
Read more >
Managing nested forms gracefully with Formik - Rajesh Naroth
Simple presentational components are easy to build. But forms.. You have so many ... We'll use the formik npm package to reduce boilerplate....
Read more >
What are your struggles when working with forms in react
Validating fields only if they are present in the form ... using context to send disabled to deeply nested form field components), ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found