I'm intrigued by the scenario where you expect a specific data type as a response from fetch / Axios / etc, but receive a different type instead. Is there a way to identify this discrepancy?
interface HttpResponse<T> extends Response {
parsedBody?: T;
}
export async function http<T>( request: RequestInfo ): Promise<HttpResponse<T>> {
const response: HttpResponse<T> = await fetch( request );
response.parsedBody = await response.json();
return response;
}
// sample code that uses the function
const response = await http<number>(
"https://thisURLdoesNotReturnANumber"
);
Would an error be thrown in this situation? Or would it fail silently? How can I detect a mismatch?