Open
Description
Which @ngrx/* package(s) are relevant/related to the feature request?
signals
Information
This RFC proposes allowing withComputed
to access methods. This would unlock more advanced computed logic and pave the way for potential future features such as withResource
.
Motivation
Currently, the withComputed
feature can only access properties and state signals of the store. Methods declared on the store instance are not yet available. This limitation prevents use cases where computed values depend on internal store logic defined as methods.
Enabling method access inside withComputed
offers several benefits:
- Better composition: Developers can define reusable methods (e.g., mappers) and use them in computed values without duplication.
- Community demand: Several users have asked for this behavior when writing advanced computed logic.
- Support for
withResource
: Resources expose methods likehasValue()
andvalue()
, which are essential for safe access. If a futurewithResource
adds these methods to the store, computed properties should be able to reference them.
Example
With this proposal implemented, developers could write:
const Store = signalStore(
// this feature does not exist yet
withResource(() =>
resource({
loader: () => Promise.resolve({ firstName: 'John', lastName: 'Doe' }),
})
),
withComputed((store) => ({
// Computed value can now call a method like hasValue()
safeUser: () =>
store.user.hasValue()
? store.user.value()
: { firstName: '', lastName: '' },
}))
);
Or reuse their own store methods inside computed values:
const Store = signalStore(
withMethods(() => ({
isValid(user: User) {
return !!user.firstName && !!user.lastName;
},
})),
withComputed((store) => ({
hasValidUser: () => store.isValid(store.user()),
}))
);
Related Issues
Alternatives Considered
- Putting methods as functions into
withProps
I would be willing to submit a PR to fix this issue
- Yes
- No