Command Palette

Search for a command to run...

Hectal
Phase 4: Unions, Literals and Narrowing
TypeScript · Exercise 6 of 6 Previous
Intermediate

Model a Request

Build the state of a data request. Define type Request as a discriminated union with status tags: "loading", "success" (with data: string[]) and "error" (with message: string).

Write render(req: Request): string that returns "Loading...", "Got N items" (N is the number of items) or "Error: <message>". End the switch with assertNever (already written for you).

Names used by the checks: Request, render.

What Check tests

  1. Your code has no type errors
  2. The console prints the three messages
  3. A success without data is rejected
  4. An error with a message is accepted
  5. assertNever is used in the switch

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Console

Nothing printed yet.