Skip to content

@ngrx/signals: Support () => T in signalMethod and rxMethod #4986

@rainerhahnekamp

Description

@rainerhahnekamp

Which @ngrx/* package(s) are relevant/related to the feature request?

signals

Information

Problem

With Angular's resource and linkedSignal, we saw that the Angular team moved having a parameter of Signal<T> to () => T. This makes sense because users don't have to create an intermediate computed signal just to pass complex values, and the tracking will be done internally.

Right now, with signalMethod or rxMethod, users need to do the following:

const printName = signalMethod<{
  firstname: string;
  lastname: string;
}>(console.log);

const firstname = signal('John');
const lastname = signal('Doe');
const name = computed(
  () => ({ firstname: ${this.firstname()}, lastname: ${this.lastname()} })
);

printName(name);

Proposed Solution

const printName = signalMethod<{
  firstname: string;
  lastname: string;
}>(console.log);

const firstname = signal('John');
const lastname = signal('Doe');

printName(() => ({ firstname: ${this.firstname()}, lastname: ${this.lastname()} }));

Advantages

  • Follows Angular's pattern: resource and linkedSignal both use () => T
  • Less boilerplate: No intermediate computed signals needed
  • Backward compatible: Signals have the same signature as () => T

Describe any alternatives/workarounds you're currently using

I have to create the computed manually.

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