Model state machines and exhaustive checking

type Result<T> =
  | { status: 'success'; data: T }
  | { status: 'error'; error: string }
  | { status: 'loading' };
 
function handle(result: Result<string>) {
  switch (result.status) {
    case 'success': return result.data;
    case 'error': return result.error;
    case 'loading': return 'Loading...';
  }
  // TypeScript ensures exhaustiveness — no default needed
}