0

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?

3
  • 1
    Yup .. it's valid./. not sure what the question is: typescriptlang.org/play?#code/… Commented Aug 4, 2022 at 8:45
  • @TitianCernicova-Dragomir Thank you. In your example in the playground you used comma in your generic type parameter <A,>. Without it it's seems to be a syntax error. What is that comma for? Commented Aug 5, 2022 at 5:03
  • 2
    In TSX files <T> is intrepreted as a JSX tag instead of a generic parmater list. The , forces it to be a generic parameter list. The playground works in TSX mode Commented Aug 5, 2022 at 16:50

1 Answer 1

1

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);
    }
  };
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.