简体   繁体   English

如何推断函数返回类型和参数名称和类型?

[英]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.

相关问题 如何用提供的参数推断函数的返回类型? - How to infer return type of function with provided arguments? 如何根据函数参数推断 Promise 的返回类型? - How to infer return type of Promise based on function arguments? 我如何让 typescript 根据 function2E977777777777777777777777777777777777777777777777777777777777777777777777777777777777BED18 的返回类型 functionC17A94F14Z 的返回类型来推断 function 的返回类型 - How do I get typescript to infer the return type of a function based on the return type on a function of its arguments 如何使 TS 在映射类型上自动推断类型 arguments? - How to make TS automatically infer type arguments on mapped types? 打字稿:正确推断包装函数的参数和返回类型 - Typescript: correctly infer arguments and return type for wrapped function 如何使用 infer 来推断 void function 的返回类型? - how to use infer to infer the return type of a void function? 如何推断泛型函数的返回类型 - How to infer return type of a generic function 预期有 3 个类型参数,但得到 1 个,但它应该推断出 2 个类型 - Expected 3 type arguments but got 1 but it should infer 2 types 一个TypeScript怎么能forEach,推断联合的类型,并改变返回类型 - How can a TypeScript forEach, infer the types of a union, and change the return type 推断同级函数的返回类型 - Infer return type of sibling function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM