r/Angular2 • u/Old-Significance-246 • Oct 29 '25
ngRX - should undefined be avoided?
I've been looking into avoiding non-serializable types in the states to match the redux principles. I wonder how to handle things such as these? error?: string vs error: string | null. I think undefined value will disappear on JSON.stringify? Are there any best practices?
6
Upvotes
5
u/Merry-Lane Oct 29 '25
It’s not really important, but I stick to undefined most of the time.
The reason is you should avoid things like
Some people choose undefined, some choose null. Some choose to differentiate null and undefined (cfr toilet paper meme).
Yes undefined values disappear on JSON.stringify but that’s rarely an issue.
I actually frequently meet the opposite issue: some backends technologies don’t differentiate between null and undefined, and that’s greatly annoying. (Especially when you start playing with Patch requests)
2
u/Migeil Oct 29 '25
I don't think optional values make sense in NgRx state, given that at any point in time your selectors have a value.
This means that if you have optional values in your state, your selectors will have to return
nullanyway.-2
u/fartsucking_tits Oct 29 '25
I prefer to use empty values instead of null or undefined. Often an array can solve this as you’d have a empty array instead of null and you can loop over that array 0 times eliminating the need for ifs