How to prevent Angular Signal references being used instead of Signal Values?
Asked Answered
B

0

6

I am enthralled with Angular Signals, exactly what I want out of the library, but one thing that I have commonly found is that I accidentally am calling a signal reference somewhere (in a template block, or in the component somewhere) instead of calling the signal value. For example:

public conditionalSignal = signal(false);
...

// Accidentally using the reference:
if (conditionalSignal) {
  ...
}

// Instead of something like this:
if (conditionalSignal()) {
  ...
}

I was hoping someone had a ESLint plugin or Angular compiler option that they knew about that would help prevent this situation from happening. This obviously doesn't occur when I am generating new signals, but when refactoring old components to use a signal based methodology, it has happened more than once, and it's something that I would like to prevent if at all possible.

I also recognize that my first example is completely valid code, but I can almost guarantee that I'd never do that intentionally.

Betide answered 22/2, 2024 at 2:8 Comment(4)
That could be a feature request on angular-eslint, to throw an error when a signal isn't invoked in a conditionKomi
@MatthieuRiegler that was exactly my thought as well if something like this didn't already exist. I was hoping that the community would come through, but I'll go submit a request on their repository and see what they say.Betide
Yes, this would be immensely helpful!Jacobs
Sadly, this boils down to the javascript conectp of thruthy and falsy. It has to be ESLint or nothing. Mabye typescript-eslint.io/rules/strict-boolean-expressions? PS: Thats why I believe the truthy/falsy concept is a billion dollar mistake.Presentment

© 2022 - 2025 — McMap. All rights reserved.