CódigoNão tentada
Wrapper de fetch tipado
Dificuldade: fácil
Escreva fetchJson<T>(url, init?), que retorna Promise<T>, lança erro em status não OK (o fetch só rejeita em erros de rede) e repassa init. Depois explique por que o generic é uma promessa ao compilador, não uma checagem em runtime.
Ver solução de referência
// The <T> only says what we expect back. Nothing checks it at runtime.export async function fetchJson<T>(url: string, init?: RequestInit): Promise<T> { const response = await fetch(url, init); if (!response.ok) { throw new Error(`HTTP ${response.status} for ${url}`); } const data = await response.json(); return data as T;}
Ctrl Enter