Skip to content

@ngrx/signals: Allow returning computation functions from withComputed callback #4782

Open
@markostanimirovic

Description

@markostanimirovic

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions