Replies: 1 comment
-
Even though it's possible to use SignalStore to build class-based stores, feature composition (functional approach) is the recommended way of using SignalStore. |
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
-
Which @ngrx/* package(s) are relevant/related to the feature request?
signals
Information
It is not possible to create class-based
signalStore
and set it's initial value from subclass. Our intention is to have common properties and methods in a store class, but also have possibility to assign additional properties from the subclass.When creating
signalStore
it is required to setwithState
, and it is not possible to assign additional properties.Issue background:
Migrating from component store and we'd like to keep our store api as similar as possible.
Initial approach:
Code above have 2 issues:
patchState
StateType
cannot be inferred (Base class expressions cannot reference class type parameters.)Describe any alternatives/workarounds you're currently using
For now, we switched back to component store, as we don't have perfect solution yet.
Potential solutions:
1. Using signalState instead of extending signalStore:
As we don't use any features of the store, other than its state, we can create
signalState
inside our custom store class.It works fine - we have access to common properties in the store, and additional properties in the component - but requires to use
state
property (which is inconsistent withsignalStore
), and we need to createinternalStore
to correctly assign base type (to use common properties inside the store class).2. Injection token for initial value:
Possible solution to not assigining additional properties is to inject initial state as a argument of
withState
Initial value is assigned correctly, but type cannot be correctly inefred, so typescript does not know about additional properties.
3. Factory for signalStore
Works fine, but forces standard api instead of class-based.
All previews available on stackblitz.com
I would be willing to submit a PR to fix this issue
Beta Was this translation helpful? Give feedback.
All reactions