I have a custom hook like this, I want to make request parameter optional and response required:
function useRequest<T, S = any>(api: (input?: S) => Promise<T>) {
const [response, setResponse] = useState<T | null>(null);
const fetchData = useCallback(
(input?: S) => {
api(input)
.then((data) => {
setResponse(data as T);
})
},
[api],
);
return { response, fetchData };
}
Basically I want to call useRequest like this:
function A()
{
//return a promise
}
function B(p1: boolean) {
//return a promise
}
useRequest<ResponseA>(A)
useRequest<ResponseB, boolean>(B)
but I cannot use second one since the boolean is not compatible with boolean | undefined.
function B(p1: boolean)tofunction B(p1?: boolean)also works