[英]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:我想在 React Native 中创建一个钩子,在它的参数中我想传递一个函数和它的变量,例如:
const useHook = (functionName, functionArgs) {...}
and my question is: how can i infer the 'functionName' args types and it's return type too.我的问题是:我如何推断“functionName”参数类型以及它的返回类型。
so that when i write useHook and i pass a function, intelesense will automatically show me the function args names and types ?这样当我编写 useHook 并传递一个函数时,intelesense 会自动显示函数参数名称和类型?
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.