I have a simple react hook that accepts one argument, an asynchronous function. It works, however I am not getting any type inference when using the hook. Specifically, I am trying to have typescript infer the type of the arguments of the callback being passed. I have provided the code below and my current usage of the function.
The hook:
import { useCallback, useState } from "react";
type UseAsync<T> = [{ result: T; loading: boolean; error: string }, (...args) => void];
export const useAsync = <T>(asyncFn: (...args) => Promise<T>): UseAsync<T> => {
const [loading, setLoading] = useState(false);
const [result, setResult] = useState(null);
const [error, setError] = useState(null);
const callback = useCallback(
async (...args) => {
try {
setLoading(true);
const result = await asyncFn(...args);
setResult(result);
} catch (e) {
setError(e);
} finally {
setLoading(false);
}
},
[asyncFn]
);
return [{ result, loading, error }, callback];
};
export default useAsync;
My usage:
const [{ result, loading, error }, postOrder] = useAsync((data: INewOrderFormData) =>
Axios.post("/api/order", data)
);
When I hover over "postOrder" in VSCode i see const postOrder: (...args: any[]) => void where args is defined as any[] when it should be referencing the types of the arguments, if any, of the function passed into the hook.