@ngrx/signals - Why can't DeepSignals handle nullability? #4772
Unanswered
james-vista
asked this question in
Q&A
Replies: 1 comment
-
|
this is fundamental to most app edvelopment. #4943 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
We've recently migrated our app's state management from
@ngrx/storeto@ngrx/signals, and it's been great. However, there's one pain point that we keep encountering, and it appears other teams in our org and also some GitHub users have been running into it as well.Issue
We commonly use optional or undefined state in our app, mostly in cases where data is not immediately available. As others have mentioned, this poses an issue with signal stores, as deep signals can't be made from undefined state:
The same issue also applies to indexed types. (
@ngrx/signals/entitiesexists for this use-case but is overkill a lot of the time.)Our workaround for this at the moment has been to invoke the signal before doing optional chaining, the obvious downside of which is losing the benefit of signals.
Discussion
These limitations are due to the fact that toDeepSignal returns a regular signal instead of a
DeepSignalwhen its value isn't a record at the time of creation.I wrote a naive alternative implementation that acts more like building a lens over a state slice using optional chaining, shown below.
The main differences in this approach are:
DeepSignalwon't take the value of the property into account, so accessing a property on aDeepSignalwill always return anotherDeepSignalDeepSignal, but also means that any arbitrary property access returns aDeepSignaltoo.SignalQuestions that I want to bring up:
Related to #4305
Beta Was this translation helpful? Give feedback.
All reactions