formik
formik copied to clipboard
Allow FormikErrors values to be type "any" for, i18n support
🐛 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 to string
. I'd like to change it back to any
, 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:
[ts] Type 'Element' is not assignable to type 'string'. [2322]
error.tsx(6, 3): The expected type comes from property 'name' which is declared here on type 'FormikErrors<Values>'
Expected behavior
The code should compile successfully, with the supplied React element assigned to formik.errors.name
.
Reproducible example
import React from 'react';
import {FormikErrors} from 'formik';
import {Trans} from '@lingui/macro';
type Values = {
name: string
}
function validate(values: Values): FormikErrors<Values> {
return {
name: <Trans>error</Trans>;
}
}
Suggested solution(s)
It looks like it's an easy fix by changing the type in types.tsx
from string
to any
.
Your environment
Software | Version(s) |
---|---|
Formik | 1.4.2 |
React | 16.7.0 |
TypeScript | 3.2.2 |
Browser | n/a |
npm/Yarn | 6.7.0 |
Operating System | Ubuntu 16.04 |