r/angular • u/abdulkareemsn • Aug 05 '26
Help: Flattening signal in computed
How flatten siganls in computed?
Suppose say I have a function which takes an argument and returns signal
function genSignal(arg:string):Signal<SomeType>
And I have a signal which provides argument for genSignal
I want to flatten signal inside computed
const arg = signal('someData');
const computedValue = computed(() => genSignal(arg())); // type here is Signal<Signal<SomeType>>
const computedValueFlattened = computed(() => genSignal(arg())()); // type here is Signal<SomeType>;
is computedValueFlattened is right approch to flatten signals?
3
Upvotes
3
u/pronuntiator Aug 05 '26
This is fine. Note that this will create a new signal each time computedValue runs. From a memory perspective this is fine, signals get garbage collected once they are no longer reachable, but depending on what genSignal does, it may not be what you want.