asynchronous – Syntax for TypeScript Arrow Functions With Generics

I don’t know if this is done properly or not, but I posted an answer in StackOverflow back in Sept. 2020 about implementing generics in TypeScript. I got downvoted 3 times, and I was wondering if the following piece of code was problematic:

This is how I defined an async generic function expression in TypeScript

const request = async <T>(param1: string, param2: number) => {
  const res = await func();
  return res.response() as T;

And a more complex pattern, in case you’d like to wrap your function inside a generic counterpart, such as memoization (Example uses fast-memoize):

const request = memoize(
  async <T>(
    url: string,
    token?: string
  ) => {
    // Perform your code here

See how you define the generic after the memoizing function.

This function has worked well for me, so I don’t see any issues.

This is the original SO question.