将类型参数传递到自定义挂钩中

Jak*_*mek 1 web typescript next.js react-hooks

我正在制作一个 nextjs 应用程序,并创建了一个自定义 useAxios 挂钩,我希望能够像使用 useState 一样进行类型断言:

const [foo, setFoo] = useState<string>(''); 
Run Code Online (Sandbox Code Playgroud)

但我想传递响应的类型:

const { data, error } = useAxios<CustomResponseType>(URL, true);
Run Code Online (Sandbox Code Playgroud)

我如何在函数中接受这种类型参数?

Nic*_*wer 6

类型签名看起来像这样:

const useAxios = <T>(URL: string, something: boolean): { data: T, error: any } => {
   // insert code here
}
Run Code Online (Sandbox Code Playgroud)

这是利用泛型