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
- Your code has no type errors
- The console prints the three messages
- A success without data is rejected
- An error with a message is accepted
assertNeveris 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.
Press Run to see the page.
Console
Nothing printed yet.