簡體   English   中英

如何推斷函數返回類型和參數名稱和類型?

[英]How to infer function return type and arguments names and types?

我想在 React Native 中創建一個鈎子,在它的參數中我想傳遞一個函數和它的變量,例如:

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

我的問題是:我如何推斷“functionName”參數類型以及它的返回類型。

這樣當我編寫 useHook 並傳遞一個函數時,intelesense 會自動顯示函數參數名稱和類型?

我試過這個,但沒有用:

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

你的函數簽名足夠好,你只是忘記返回結果:

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

或縮短:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM