I've encountered an issue while implementing signals and computed in my new Angular project.
There's a computed value that holds an id, which is initially not set and will be assigned by user interaction. To handle this initial state, I attempted to set it to null or undefined.
public someSignal = signal<number | null>(null);
public selectedId = computed(() => {
var result: number | null = null;
let value = someSignal();
// perform some computations
return result;
});
I wanted to use this functionality in the template.
<div *ngIf="selectedId() as id">Current id: {{ id }}</div>
However, I noticed that this only works when the id is not 0. If the id is zero, it behaves as if it is null. I tried changing everything to number | undefined, but encountered the same issue. It seems that null >= 0 evaluates to true, but I'm unsure if there have been any changes in how signals and computed values are handled.
I could work around this by using -1 to represent "null," but I would appreciate knowing if there are better solutions for working with number | null, or if this is something we simply have to deal with.
Thank you for your help.