`@ngrx/signals`: Allow returning computation functions from `withComputed` callback
Which @ngrx/* package(s) are relevant/related to the feature request?
signals
Information
Provide the ability to return computation functions from withComputed callback in addition to signals to reduce repetitive code in the most common use case:
const CounterStore = signalStore(
withState({ count: 0 }),
withComputed(({ count }) => ({
doubleCount: () => count() * 2,
})),
);
If the computed signal needs to be used before it's returned or if it has custom equal function, it's still necessary to explicitly use computed API:
const CounterStore = signalStore(
withState({ count: 0 }),
// 1: custom equal
withComputed(({ count }) => ({
doubleCount: computed(() => count() * 2, { equal: /* ... */ }),
})),
// 2: using computed signal before returning it:
withComputed({ count }) => ({
const doubleCount = computed(() => count() * 2);
return {
doubleCountPlus1: () => doubleCount() + 1,
doubleCount,
};
})),
);
Describe any alternatives/workarounds you're currently using
No response
I would be willing to submit a PR to fix this issue
- [ ] Yes
- [ ] No
I would gladly have a look at this in a few days, if nobody implemented it until then.
@iliev-ilian Issues with the Core/Collaborators Work label will be handled by the NgRx team. If you're interested in contributing, you can search for issues that have the Accepting PRs label.
In order to speed up the NgRx v20 release, I've taken the liberty of creating a PR for this one.