Home > other >  on TypeScript, on a generic function declaration can we use ReturnType and Parameters
on TypeScript, on a generic function declaration can we use ReturnType and Parameters

Time:11-08

I am trying to write a generic wrapper function, and thought that this would work, but it is not:

function wrapFunctionWithLogging<T extends Function>(f: (...args: Parameters<T>) => ReturnType<T>): ReturnType<T>  {
      return function (...args: Parameters<T>) {
        console.log('before ');
        try {
          return f(...args);
        } catch (error) {
          console.error(error);
          throw error;
        } finally {
          console.log('done');
        }
      };
    }

I got another version with two types like

wrapFunctionWithLogging<TArgs extends any[], TReturn> 

and it is working fine, I am curious why this version would not work.

CodePudding user response:

Use (...args: any[]) => any to describe any function instead. Your wrapper should take a function with this signature and return a function with the same signature, so they're actually both simply T. You also need to assert that the return type is T as well.

function wrapFunctionWithLogging<T extends (...args: any[]) => any>(f: T): T {
    return function (...args: any[]) {
        console.log("before");
        try {
            return f(...args);
        } catch (error) {
            console.error(error);
            throw error;
        } finally {
            console.log("done");
        }
    } as T;
}

Playground

CodePudding user response:

building on the idea of extending from a function with arguments seems to work, without type assertion:)

function wrapFunctionWithLogging<T extends (...args: any[]) => any>(f: T): (...args: Parameters<T>) => ReturnType<T> {
  return function (...args: Parameters<T>): ReturnType<T> {
    console.log('before ');
    try {
      return f(...args);
    } catch (error) {
      console.error(error);
      throw error;
    } finally {
      console.log('done');
    }
  };
}

TS Fiddle here

  • Related