how to explicitly assert the type of a generic function?

Viewed 26

Say I have the type of a generic function defined somewhere:

type FArA = <T>(t: T)=>T // function taking type T and returning type T

I then define a function of that type:

function f<T>(a: T):T {return a;}

I know of no syntax to succinctly declare (and have the compiler typecheck) that f is indeed of type FArA. The only workarounds I can think of are to use an otherwise needless assignment:

const g: FArA = f;

… or to take the function expression approach (as advised in this SO answer) whose syntax, however I find odd:

const f2: FArA = <T,>(a: T):T=>a

Is there another way?

playground

0 Answers
Related