type AsyncState<T> =
| { 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<T>(s: AsyncState<T>): 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
}
}
Create a free account and build your private vault. Share publicly whenever you want.