Exhaustive UI state union

Handle every loading, ready and failed state with an exhaustive switch.

Original Pearto instructional example. Test it in your own environment and adapt it to your requirements.

type State = {kind:'loading'} | {kind:'ready'; count:number} | {kind:'failed'; message:string};
function label(state:State):string {
  switch (state.kind) {
    case 'loading': return 'Loading';
    case 'ready': return state.count + ' items';
    case 'failed': return state.message;
    default: {const unreachable:never = state; return unreachable;}
  }
}