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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type definition of FormikErrors should allow nulls #2095

Open
KrzysztofMadejski opened this issue Dec 9, 2019 · 0 comments
Open

Type definition of FormikErrors should allow nulls #2095

KrzysztofMadejski opened this issue Dec 9, 2019 · 0 comments
Labels

Comments

@KrzysztofMadejski
Copy link

馃悰 Bug report

Current Behavior

If we are validating a value being a list (ie. supported by FieldArray) then some errors on the list might be null (ie. [null, "error"]), but current typescript definitions don't allow for that.

Expected behavior

Following code (along sandbox don't produce typescript compiler errors):

setErrors({friends:[null,{"name":"too short"}]})

Reproducible example

https://codesandbox.io/s/formik-fieldarray-sb4ql

Suggested solution(s)

formik/src/types.tsx

Lines 13 to 21 in 5c59dbe

export type FormikErrors<Values> = {
[K in keyof Values]?: Values[K] extends any[]
? Values[K][number] extends object // [number] is the special sauce to get the type of array's element. More here https://github.com/Microsoft/TypeScript/pull/21316
? FormikErrors<Values[K][number]>[] | string | string[]
: string | string[]
: Values[K] extends object
? FormikErrors<Values[K]>
: string;
};

should allow nulls in array of errors:

export type FormikErrors<Values> = {
  [K in keyof Values]?: Values[K] extends any[]
    ? Values[K][number] extends object // [number] is the special sauce to get the type of array's element. More here https://github.com/Microsoft/TypeScript/pull/21316
      ? (FormikErrors<Values[K][number]>|null)[] | string | (string|null)[]
      : string | (string|null)[]
    : Values[K] extends object
    ? FormikErrors<Values[K]>
    : string;
};

Your environment

Software Version(s)
Formik 2.0.6
@stale stale bot added the stale label Feb 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant