简体   繁体   中英

How to infer function return type and arguments names and types?

I want to create a hook in react native, and in it's params i want to pass a function and it's variables, EX:

const useHook = (functionName, functionArgs) {...}

and my question is: how can i infer the 'functionName' args types and it's return type too.

so that when i write useHook and i pass a function, intelesense will automatically show me the function args names and types ?

i tryed this but didn't work:

const useHook = <T, R>(fun: (args: T) => R, args: T) => {
  fun(args);
}

Your function signature is good enough, you just forgot to return the result :

const useHook = <T, R>(fun: (args: T) => R, args: T) => {
  return fun(args);
}

Or shortend :

const useHook = <T, R>(fun: (args: T) => R, args: T) => fun(args);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM