Open agwells opened 5 years ago
Yeah this has been suggested before. I wonder if there is a way to maybe uses generic here but default it to string.
Good idea! Since FormikErrors
is already a generic, it'd be simple enough to add a second generic parameter for the type of the errors, and give it a default type. I think something like this should work:
export type FormikErrors<Values, ErrorMessage = string> = {
[K in keyof Values]?: Values[K] extends object
? FormikErrors<Values[K]>
: ErrorMessage
};
I'll update my merge request.
It turns out it's not as simple as adding a second parameter to FormikErrors
. In order for the new error message type to be available in places like setFieldError
, for example, you also wind up having to parameterize FormikActions
, which means you need to parameterize FormikProps
, etc, until nearly every type in the library has an extra parameter for the error message type. See: https://github.com/jaredpalmer/formik/pull/1293/commits/30206b0e75d6836f7604506b9b4ee77a280697a9
I'm pretty new to TypeScript, though, so maybe I'm missing a simpler solution?
This is why it’s been avoided in the past. Types are a balancing act between safety and verbosity.
In that case, I guess I go back to proposing we just loosen the type for error messages, from string
to any
. :)
I think this is still unresolved. The only type of error allowed right now is string which is really limiting.
Has anyone come by a workaround on this issue?
:D any fixes ?
🐛 Bug report
It looks like at one point, the FormikErrors type allowed errors to be type
any
, which was added in bug #266, to support i18n frameworks. But over the course of a couple of TypeScript upgrades, this got dropped, and the type was changed tostring
. I'd like to change it back toany
, to once again make it easier to use with i18n frameworks (or just to put other React elements in error messages).Current Behavior
When I try to return a React element as an error in a Formik validate() method, I receive a TypeScript error, saying that it must be a string:
Expected behavior
The code should compile successfully, with the supplied React element assigned to
formik.errors.name
.Reproducible example
Suggested solution(s)
It looks like it's an easy fix by changing the type in
types.tsx
fromstring
toany
.Your environment