With algebraic data type like in Haskell, the state could be typed like this:
data State a =
Loading
| Failed
| Loaded [a]
Compared to a representation like `{ loading:true, error: false, pics:[] }`, the above model has the benefit of "making illegal state not representable".
One library in js I like to use to model this type of tagged union / ADT values is single-key[1]. Each variant in a tagged union type is represented by an plain object with just one key, for example:
One library in js I like to use to model this type of tagged union / ADT values is single-key[1]. Each variant in a tagged union type is represented by an plain object with just one key, for example:
The library offers pattern matching syntax on top of this data representation: [1] https://www.npmjs.com/package/single-key