×

typescript · level 2 · unit 7 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Real apps fetch, check, and plan for the edge cases.

A real app joins everything up. It fetches data from the practice server, checks the status, reads the JSON as unknown, validates each item and then works with typed values. When something is wrong, it throws, and one catch explains the problem kindly.

Plan for each , an unusual situation that still happens: the address is wrong, the list is empty, or one item is broken. A good app copes with all of them without crashing.

async function main(): Promise<void> {
  const reply = await fetch("https://api.example.com/names");
  if (!reply.ok) throw new Error(`Server said ${reply.status}`);
  const data: unknown = await reply.json();
  if (!Array.isArray(data)) throw new Error("Expected a list");
  const names = data.filter((item) => typeof item === "string");
  console.log(`${names.length} good names:`, names);
}
main();

Test each edge case on purpose: a wrong address, an empty list, a broken item.