Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to dry up type definition of function that returns another function

I’ve enabled the ESLint rule that forces you to specify all functions’ return types. In React hooks, this leads to patterns such as the following:

const useSubmitForm = (): ((
  form: FormProps,
  submissions: (string | MultipleChoiceOption[])[],
  user: string
) => FormEventHandler) => {
  const notify = useNotification();
  return (
      form: FormProps,
      submissions: (string | MultipleChoiceOption[])[],
      user: string
    ): FormEventHandler =>
    (e) => {

    };
};

The types of the outer function’s return type arguments are identical to the inner function’s arguments.

How can I dry this up so as to avoid the repetition?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In this case just delete the type definitions of the function innards

const useSubmitForm = (): ((
  form: FormProps,
  submissions: (string | MultipleChoiceOption[])[],
  user: string
) => FormEventHandler) => {
  const notify = useNotification();
  return (
    form,
    submissions,
    user
  ): FormEventHandler =>
    (e) => {

    };
};

TypeScript automatically gets those types. Playground link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading