Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot pipe on combineLatest

Tags:

angular

rxjs

I am trying to follow this example but am running into an issue when using combineLatest():

Property 'subscribe' does not exist on type 'OperatorFunction<unknown, [unknown, Item[], User[]]>'

Here is my facade:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { combineLatest, distinctUntilChanged, map } from 'rxjs/operators';

export interface User {
  test: string;
}

export interface Item {
  test: string;
}

interface State {
  items: Item[];
  users: User[];
}

let _state: State = {
  items: [],
  users: [],
};

@Injectable()
export class Facade {
  private store = new BehaviorSubject<State>(_state);
  private state$ = this.store.asObservable();

  items$ = this.state$.pipe(map(state => state.items), distinctUntilChanged());
  emails$ = this.state$.pipe(map(state => state.users), distinctUntilChanged());

  constructor(private http: HttpClient) {
    combineLatest(this.items$, this.emails$).subscribe(
      ([items, users]) => {}
    );
  }
}

items$ and emails$ are both Observables. This code nearly mirrors the code in the example above, why am I getting this error?

like image 833
user1222324562 Avatar asked Dec 14 '22 11:12

user1222324562


1 Answers

Assuming you're using version 6+ of RxJS, as per the migration docs:

  1. combineLatest from rxjs/operators has been deprecated. You should now import the method directly:

    import { combineLatest } from 'rxjs';
    
  2. The new version of combineLatest() accepts an array of Observables as its argument:

    combineLatest([this.items$, this.emails$]).subscribe(([items, emails]) => {});
    
like image 112
Andrew Hill Avatar answered Feb 15 '23 06:02

Andrew Hill