Describe Each State of Remote Data

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Data fetched from a server is in one of four states: idle, loading, success (with its data) or error (with a message). Export a generic type RemoteData<T> for them, told apart by status, and write describeState(state) for a list of names: Not loaded yet, Loading, 1 item or 3 items (the count of the data), or Failed: followed by the message. Type it so data can only be read once status is success, and message only once it's error.

Your code must pass

  • Describes idle and loading
  • Counts the items on success
  • Shows the error message
  • Has no type errors
  • RemoteData has the four states
  • data has its type on success
  • data can't be read before checking status