Skip to content

RFC(@ngr/signals): Allow withComputed to access store methods #4846

Open
@rainerhahnekamp

Description

@rainerhahnekamp

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 like hasValue() and value(), which are essential for safe access. If a future withResource 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

#4650, #4499, #4500

Alternatives Considered

  • Putting methods as functions into withProps

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