Command Palette

Search for a command to run...

Hectal
Phase 14: TypeScript with React
TypeScript · Exercise 6 of 6 Previous
Advanced

Loading, error and success as one type

Replace the loose Remote<T> with a discriminated union: { status: "loading" }, { status: "error"; message: string } and { status: "success"; data: T }.

Then write RemoteView: it shows Loading..., Failed: <message> or whatever render(data) returns, using a switch that ends with default: return assertNever(state);.

The checks use Remote, the ids #s-loading, #s-error and #s-success, and the text each should contain.

What Check tests

  1. Your code has no type errors
  2. A success state without data is rejected
  3. A success state with the wrong kind of data is rejected
  4. An error state without a message is rejected
  5. A valid error state is accepted
  6. The ending of RemoteView is assertNever(state)
  7. The three views show Loading..., Failed: boom and Hello Asha

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.

Your page

Press Run to see the page.

Console

Nothing logged yet.