Formik: Add generics to ArrayHelpers type

Created on 16 Sep 2018  路  4Comments  路  Source: formium/formik

Current Behavior

The current ArrayHelpers type does not use generics in its type.

Desired Behavior

It would be nice if the ArrayHelpers added generics so that we could type check the inner methods as well.

Suggested Solution

export type FieldArrayRenderProps<T = any, Values = any> = ArrayHelpers<T> & {
  form: FormikProps<Values>;
  name: string;
};

interface ArrayHelpers<T = any> {
  push: (obj: T) => void;
  handlePush: (obj: T) => () => void;
  swap: (indexA: number, indexB: number) => void;
  handleSwap: (indexA: number, indexB: number) => () => void;
  move: (from: number, to: number) => void;
  handleMove: (from: number, to: number) => () => void;
  insert: (index: number, value: T) => void;
  handleInsert: (index: number, value: T) => () => void;
  replace: (index: number, value: T) => void;
  handleReplace: (index: number, value: T) => () => void;
  unshift: (value: T) => number;
  handleUnshift: (value: T) => () => void;
  handleRemove: (index: number) => () => void;
  handlePop: () => () => void;
  remove(index: number): T | undefined;
  pop(): T | undefined;
}

Who does this impact? Who is this for?

  • No impact if we use defaults for the generics
  • Allows users to strongly type the arrayHelpers prop if desired.

I'm willing to submit a PR for this if this sounds ok.

stale

Most helpful comment

All 4 comments

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

@jaredpalmer just wondering if this is something you'd like me to work on. Happy to do it (or not do it). 馃槃

I also would like this. Currently I'm trying to figure out some type hijinks to force form to have the type of the form to get type checking when I access it within my FieldArray block, or just passing the form prop down from the outer Formik render prop because it's typed. But neither as as ideal as just being able to type the arrayHelpers.

I was coming here to offer to make my own PR about this issue, but found this currently existing. I know you're in the thick of v2 prep but let me or @nzacca know if you'd like a PR for this @jaredpalmer

Was this page helpful?
0 / 5 - 0 ratings

Related issues

green-pickle picture green-pickle  路  3Comments

dfee picture dfee  路  3Comments

emartini picture emartini  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments

giulioambrogi picture giulioambrogi  路  3Comments