0

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

Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
  • Is it only odd because of ``? The extra comma is there so it isn't confused with a JSX tag. – kelsny Sep 24 '22 at 13:17
  • 1
    There is no other way, but in 4.9 we have `satisfies`, which will look like [this](https://tsplay.dev/WkKQ9W). Frankly, this isn't a big problem to most users. Is this just something that is inconvenient? – kelsny Sep 24 '22 at 13:21
  • @caTS thanks, I understand why the extra comma is needed. Nothing to lose sleep over, just wanted to make sure that I am not missing any syntax. – Marcus Junius Brutus Sep 24 '22 at 13:50
  • Yes, you could also declare your method using an interface. It's a bit more verbose but, `interface MyFArA { f2: FArA }`. – beautifulcoder Sep 24 '22 at 20:19

0 Answers0