Skip to content

Prevent void in computed signals #66720

@undsoft

Description

@undsoft

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

core

Description

If you don't return anything from the computed signal, no one will complain about it. There will be no compilation or runtime errors.

It is, however, likely a mistake which will lead to incorrect behavior.

user = signal({
  firstName: 'Lightning',
  lastName: 'McQueen',
});

fullName = computed(() => {
  // Oops, I forgot to add return
  `${this.user().firstName} ${this.user().lastName}`;
});
Hi, {{ fullName() }},

Proposed solution

I cannot think of a legitimate reason not to return anything from the computed signal, so it seems like it should be typed to exclude void from the list of types it accepts.

Alternatives considered

@typescript-eslint/no-unused-expressions can help, but it only catches simple cases.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions