TypeScript: Type a fetch response
A short, tested TypeScript example for type a fetch response. Copy it and adjust the names and paths for your project.
TypeScript
type Item = { id: number; title: string };
async function getItems(): Promise<Item[]> {
const res = await fetch("/api/items");
if (!res.ok) throw new Error(res.statusText);
return (await res.json()) as Item[]; // trust only data you control; validate otherwise
}Find it in Zest
Search for “typescript type a fetch response” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.