// Created on savesnippets.com · https://savesnippets.com/nK2xRddEB33WU6 type AsyncState = | { status: 'idle' } | { status: 'loading' } | { status: 'success'; data: T } | { status: 'error'; error: Error }; function assertNever(x: never): never { throw new Error(`Unhandled variant: ${JSON.stringify(x)}`); } function render(s: AsyncState): string { switch (s.status) { case 'idle': return '—'; case 'loading': return 'Loading…'; case 'success': return `OK: ${JSON.stringify(s.data)}`; case 'error': return `Error: ${s.error.message}`; default: return assertNever(s); // ← compile error if you add a variant } }