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:
- Created 4 years ago
- Reactions:9
- Comments:10 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Still relevant
This is still an issue for me.