Type of the parsed response data (only on the ok: true branch)
Type of request headers used in the config
Type of request params used in the config
Type of response headers received from the server
// Narrow on `ok` before destructuring `data`
const response = await api.get<User>('/users/123');
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
const { data: user } = response;
// Narrow on `ok` before trusting `data` as `T`
const [response, err] = await attempt(() => api.get<User[]>('/users'));
if (err) {
console.error('Transport failure:', err);
return;
}
if (!response.ok) {
console.warn('Non-2xx response:', response.status, response.data);
return;
}
console.log('Success:', response.data);
Enhanced response object that provides comprehensive information about a fetch request and its result.
A discriminated union on
ok: a 2xx exchange narrowsdatatoT, while a non-2xx exchange narrowsdatatounknown— the compiler forces theres.okcheck beforedatacan be trusted asT. Every completed HTTP exchange resolves this way; only a transport failure (abort, timeout, connection lost, parse failure onok: true) rejects as aFetchError.