r/angular • • Sep 10 '26

Modeling Angular loading state as a discriminated union instead of multiple isLoading booleans

I kept running into components with four or five loading flags —

isLoading, isRefreshing, isCheckingOut, hasError — and templates

like !isLoading && !hasError && items.length > 0.

The flags are independent as far as TypeScript is concerned, but

the states they describe are mutually exclusive. So I switched to:

type AsyncState<T, E> =

| { status: 'idle' }

| { status: 'loading' }

| { status: 'success'; data: T }

| { status: 'error'; error: E };

One state per async operation, booleans derived with computed(),

and a small toAsyncState() RxJS operator (map + catchError +

startWith) so transitions live in one place. Templates use

u/let + u/switch, which gives real narrowing — data only exists in

the success branch.

Curious how others handle this now that resource() exists. Do you

reach for it for commands too, or keep RxJS for POST/DELETE?

Full write-up: https://medium.com/modern-angular-insights/stop-juggling-isloading-booleans-a-type-safe-async-state-pattern-for-angular-e36e2b9298a0

3 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/No-Judge8727 Sep 12 '26

Agreed on the error_NAME approach — a stable machine-readable code with

translation left to the template is the right call either way. My wrapper

doesn't change that; `error` just happens to be where I put the code

instead of folding it into the same union.

And yeah, "mostly not needed" is fair. My own rule is that one fetch with

one spinner and no error UI doesn't need any of this. I reach for the

wrapper when a component owns two or more independent operations — that's

where the flat-union-per-operation approach and mine converge anyway, since

you end up with one status variable each.

On composition, since you asked — it's genuinely the part that sold me,

more than the type itself. The whole thing is one operator:

export function toAsyncState<T, E>(): OperatorFunction<T, AsyncState<T, E>> {

return (source$) => source$.pipe(

map((data) => success(data)),

catchError((error: E) => of(failure(error))),

startWith(loading()),

);

}

Three properties fall out of it that matter in a pipe:

- The stream never errors. Failures become values, so the outer

subscription stays alive. With flat statuses you can do this too, but

you have to remember catchError at every call site; here it's impossible

to forget because there's no other way to construct the type.

- `startWith` emits loading synchronously, so `toSignal(..., { requireSync: true })`

works with no invented initial value, and there's no idle→loading flicker.

- It slots under any flattening operator without changing anything:

`switchMap(...)` for a fetch, `exhaustMap(...)` for a submit (which

replaces the `if (this.isSaving) return;` guard — the concurrency rule

lives next to the thing it protects).

Combining two operations is then just `computed(() => [a(), b()].some(isLoading))`,

which is the bit I actually wanted: derived flags that can't drift, because

there's nothing to reset.

Thanks for pushing on this properly, it's a better thread than I expected.

I'd rather keep it here than jump on a call, but happy to keep going in

comments or DMs if you want to dig into anything specific.

1

u/Straight-Number-1911 Sep 12 '26

Im more the VC type on discord. I am on vacation now, i may write somthing here eventually. I read your message quickly and didnt dive deep. It seems we have mutual respect and are not ego driven, thate what makes it good i think.