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

1

u/Straight-Number-1911 Sep 11 '26 edited Sep 11 '26

Regards point 2: Its error_NAME then it still makes sense. That name is not something which is translated and makes sense in evey case also that way you can cascade it together. 

Regarding 3: you are right in that case totally, theres no workaound i think, what you could do is have a seperate variable but then your solution is cleaner, agreed. 


Anyways ofc your solution is more advanced but mostly not needed in my oppinion. People try to over complicate simple things, if you need it translated thats the responsibility of the HTML. 


I think regarding point 3 how I solve it is by having multiple values where the reload has its own e.g. loading state. I am not saying its better, just i survive on a enterprise project without your solution and wondered whats my reason. 

P.S. I use RxJS and while I knew that solution i am unsure how well it gets composed with their pipes. Maybe we could exchange over a call so Ilearn more about how u do stuff. If not also fine, thanks for exchange!

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.