Skip to content

Commit

Permalink
FieldArray - make array helpers more type-safe (#3982)
Browse files Browse the repository at this point in the history
Currently used `push<X = T[number]>(obj: X): void;` allows to push `string` to `number[]`. By changing the signature to  `push<X extends T[number] = T[number]>(obj: X): void;` it won't be possible any more.
  • Loading branch information
burtek authored Aug 29, 2024
1 parent c798145 commit 51e8cbb
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions packages/formik/src/FieldArray.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export type FieldArrayConfig = {
} & SharedRenderProps<FieldArrayRenderProps>;
export interface ArrayHelpers<T extends any[] = any[]> {
/** Imperatively add a value to the end of an array */
push<X = T[number]>(obj: X): void;
push<X extends T[number] = T[number]>(obj: X): void;
/** Curried fn to add a value to the end of an array */
handlePush<X = T[number]>(obj: X): () => void;
handlePush<X extends T[number] = T[number]>(obj: X): () => void;
/** Imperatively swap two values in an array */
swap: (indexA: number, indexB: number) => void;
/** Curried fn to swap two values in an array */
Expand All @@ -42,25 +42,25 @@ export interface ArrayHelpers<T extends any[] = any[]> {
/** Imperatively move an element in an array to another index */
handleMove: (from: number, to: number) => () => void;
/** Imperatively insert an element at a given index into the array */
insert<X = T[number]>(index: number, value: X): void;
insert<X extends T[number] = T[number]>(index: number, value: X): void;
/** Curried fn to insert an element at a given index into the array */
handleInsert<X = T[number]>(index: number, value: X): () => void;
handleInsert<X extends T[number] = T[number]>(index: number, value: X): () => void;
/** Imperatively replace a value at an index of an array */
replace<X = T[number]>(index: number, value: X): void;
replace<X extends T[number] = T[number]>(index: number, value: X): void;
/** Curried fn to replace an element at a given index into the array */
handleReplace<X = T[number]>(index: number, value: X): () => void;
handleReplace<X extends T[number] = T[number]>(index: number, value: X): () => void;
/** Imperatively add an element to the beginning of an array and return its length */
unshift<X = T[number]>(value: X): number;
unshift<X extends T[number] = T[number]>(value: X): number;
/** Curried fn to add an element to the beginning of an array */
handleUnshift<X = T[number]>(value: X): () => void;
handleUnshift<X extends T[number] = T[number]>(value: X): () => void;
/** Curried fn to remove an element at an index of an array */
handleRemove: (index: number) => () => void;
/** Curried fn to remove a value from the end of the array */
handlePop: () => () => void;
/** Imperatively remove and element at an index of an array */
remove<X = T[number]>(index: number): X | undefined;
remove<X extends T[number] = T[number]>(index: number): X | undefined;
/** Imperatively remove and return value from the end of the array */
pop<X = T[number]>(): X | undefined;
pop<X extends T[number] = T[number]>(): X | undefined;
}

/**
Expand Down

0 comments on commit 51e8cbb

Please sign in to comment.