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.

News & updates

No news stories have been linked to this profile.

Search news coverage ↗

Sources & useful links

No publisher or independent source links have been added to this entry.

Suggest a source or correction ↗
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;}
  }
}