Home > Software design >  How to define a generic anonymous function type in TypeScript?
How to define a generic anonymous function type in TypeScript?

Time:08-05

Is it possible to have Generic Anonymous function type?

I was reading this article and found this piece of code.

import { Eq } from 'fp-ts/Eq'

export const not = <A>(E: Eq<A>): Eq<A> => ({
  equals: (first, second) => !E.equals(first, second)
})

Is not function here even a valid typescript syntax?

CodePudding user response:

This code is perfectly fine ,

It's (almost) the equivalent of this generic function but with an arrow function definition :

function not2<A>(E: Eq<A>): Eq<A> {
  return {
    equals: function (first, second) {
       return !E.equals(first, second);
    }
  };
}
  • Related