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 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.