I am working on a hook here but have one remaining problem.
<T extends Constraint> is flagging a warning.
The warning is: Type parameter 'T' cannot be inferred from any parameter.
Constraint looks like this:
export type Constraint = {
[key: string]: unknown;
[index: number]: never;
};
Its implementation looks like the below, and within this code is where the warning is flagged:
import { useAuth0 } from '@auth0/auth0-react';
import { useEffect, useState } from 'react';
import { type Constraint, type Response } from './useFetch.type';
export const useFetch = <T extends Constraint>(
url: string,
): {
data: Response<T> | null;
error: Error | null;
loading: boolean;
} => {
const [data, setData] = useState<Response<T> | null>(null);
const [error, setError] = useState<Error | null>(null);
const [loading, setLoading] = useState(false);
const { getAccessTokenSilently } = useAuth0();
useEffect(() => {
(async () => {
const accessToken = await getAccessTokenSilently();
try {
setLoading(true);
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error ${response.status}`);
}
const data: Response<T> = await response.json();
setData(data);
} catch (error: unknown) {
setError(error instanceof Error ? error : new Error(String(error)));
} finally {
setLoading(false);
}
})();
}, [url, getAccessTokenSilently]);
return { data, error, loading };
};
Can anyone suggest how to satisfy the warning?
