You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** Really the path to the array field to be updated */
name: string;
/** Should field array validate the form AFTER array updates/changes? */
validateOnChange?: boolean;
}&SharedRenderProps<FieldArrayRenderProps>;
exportinterfaceArrayHelpers<Textendsany[]=any[]>{
Desired Behavior
T of ArrayHelpers<T> should be inferred from FieldArrayRenderProps.
Suggested Solution
Maybe we can leverage knowledge from Get from type-fest to achieve something like:
importtype{Get,Paths}from'type-fest';import{LiteralStringUnion}from'type-fest/source/literal-union';import{ToString}from'type-fest/source/internal';typePath<T>=LiteralStringUnion<ToString<Paths<T,{bracketNotation: false}>|Paths<T,{bracketNotation: true}>>>exporttypeFieldArrayRenderProps<Values=FormikValues,NameextendsPath<Values>=any>=ArrayHelpers<Get<Values,Name>extendsany[] ? Get<Values,Name> : any>&{form: FormikProps<Values>;name: Name;};exporttypeFieldArrayConfig<Values=FormikValues,NameextendsPath<Values>=any>={/** Really the path to the array field to be updated */name: Name;/** Should field array validate the form AFTER array updates/changes? */validateOnChange?: boolean;}&SharedRenderProps<FieldArrayRenderProps<Values,Name>>;exportinterfaceArrayHelpers<Textendsany[]=any[]>{
Who does this impact? Who is this for?
A type-safer <FieldArray /> or FieldArrayRenderProps can improve the type coverage of the package and avoid lots of unsafe cases.
Corner cases
For nested object
typeValues={friends: string[];friendsObj: {name: string;friends: string[];friendsObj: {name: string;}[];}[];};typecase3=ArrayHelpers<Get<Values,'friendsObj[0].friendsObj'>>// will be ArrayHelpers<any> since `friendsObj[0].friendsObj` can be undefined.// allow ArrayHelpers<T extends (any[] | undefined) = any[]> can be problematic
The text was updated successfully, but these errors were encountered:
Feature request
Hi team, I'd like to propose an improvement on
<FieldArray />
orFieldArrayRenderProps
to make it type-safer.Current Behavior
Currently, we only have generic type on
ArrayHelpers
formik/packages/formik/src/FieldArray.tsx
Lines 20 to 31 in 2618cc4
Desired Behavior
T
ofArrayHelpers<T>
should be inferred fromFieldArrayRenderProps
.Suggested Solution
Maybe we can leverage knowledge from
Get
from type-fest to achieve something like:Who does this impact? Who is this for?
A type-safer
<FieldArray />
orFieldArrayRenderProps
can improve the type coverage of the package and avoid lots of unsafe cases.Corner cases
For nested object
The text was updated successfully, but these errors were encountered: