1

I would like to expose an observable in my service, which emits a value each time a BehaviorSubject is assigned a value (and after filtering it from a list). Example implementation:

export class MyService {

   // Given a list of all object
   private readonly allObjects$: Observable<SomeObject[]>;  
   // An id of a SomeObject instance
   private readonly mySubject = new BehaviorSubject<string|undefined>(undefined);

   // Expose a single instance from the list of all objects.
   public readonly myObject$: Observable<SomeObject|undefined>;   

   constructor() {

     
     this.myObject$ = 

        // Pipe in the object id (i.e.: 123)
        this.mySubject.pipe(

        // Add the list of all objects
        withLatestFrom(this.allObjects$),

        // Filter out the object whose id is 123 from the list of objects. This filtered 
        // object should be the value emitted by myObject$.
        switchMap(
            (info: [string, SomeObject[]]) =>
                info[1].filter(t => t.name === info[0])));
   }
}

Usage:

  mySubject.next('123')  
  this.myObject$.subscribe(console.log)  // prints: SomeObject(with id 123)

However the above snippet produces this error (for the withLatestFrom operator):

Argument of type 'OperatorFunction<string | undefined, [string | undefined, 
SomeObject[]]>' is not assignable to parameter of type 'OperatorFunction<string | 
undefined, [string, SomeObject[]]>'.

What am I doing wrong? How do I fix this?

GK100
  • 3,664
  • 1
  • 26
  • 19

2 Answers2

2

You have defined BehaviorSubject to hold either string or undefined value. So the info array defined within switchMap can have the 0th element as either string or undefined, and hence the type definition needs to specified accordingly. It should be:

    info: [string | undefined, SomeObject[]]
Siddhant
  • 2,911
  • 2
  • 8
  • 14
0

You don't need a higher order observable to subscribe to another observable for this use case, instead of using switchMap(), why don't you use map() or filter() ?

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 01 '22 at 15:10