103

After running the rxjs migration tool using

rxjs-5-to-6-migrate -p src/tsconfig.app.json

I'm now getting a linting error:

combineLatest is deprecated: Deprecated in favor of static combineLatest.

Here is my code before running the migration command:

this.store.combineLatest(
        this.store.select(lang.getCurrent),
        this.store.select(lang.getCurrentLocale)
    ).subscribe(([state, currentLang, locale]) => {
        this._language = session.language === currentLang ? '' : currentLang;
        this._locale = session.locale === locale ? '' : locale;
    });

My code after running the migration command: (currently presenting a linting error)

import {map, combineLatest} from 'rxjs/operators';
this.store.combineLatest(
        this.store.select(lang.getCurrent),
        this.store.select(lang.getCurrentLocale)
    ).subscribe(([state, currentLang, locale]) => {
        this._language = session.language === currentLang ? '' : currentLang;
        this._locale = session.locale === locale ? '' : locale;
    });

The question was asked in this stackoverflow questions, but it was not specific enough: Angular 6 ng lint duplicate errors and warnings, combineLatest is deprecated .

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Samer
  • 3,848
  • 4
  • 25
  • 24

6 Answers6

135

In rxjs 6.5+

import { combineLatest } from 'rxjs';

combineLatest([a$, b$, c$]);

And for most applications it's helpful to map the array of observables to a new value as well:

combineLatest([a$, b$, c$]).pipe(
  map(([a$, b$, c$]) => ({
    a: a$, 
    b: b$, 
    c: c$
  }))
);

Also see: https://www.learnrxjs.io/learn-rxjs/operators/combination/combinelatest

Community
  • 1
  • 1
ofir fridman
  • 2,691
  • 2
  • 17
  • 27
105

Deprecated!

Please refer to ofir fridman's answer for the correct syntaxs as of RxJs 6.5


I found an answer in this article titled: RxJS 6: What's new and what has changed? ( which comes from official docs):

The solution is to convert:

import { combineLatest } from 'rxjs/operators';

a$.pipe(combineLatest(b$, c$));

into:

import { combineLatest } from 'rxjs';

combineLatest([a$, b$, c$]);
Vedran
  • 10,369
  • 5
  • 50
  • 57
Samer
  • 3,848
  • 4
  • 25
  • 24
  • 13
    or if you want to preserve combineLatest inside your pipe, you can do `a$.pipe(a$ => combineLatest(a$,b$, c$));` – Twen Nov 20 '18 at 08:36
  • 5
    It would appear that this solution is now deprecated too. What's the new new way? – UncleBob Sep 09 '19 at 09:52
  • 2
    I updated the answer. Please let us know if ofir fridman's answer is out of date. – Samer Sep 14 '19 at 23:08
  • 4
    I am using rxjs version 6.5.3 and idea ultimate 2020.1.2. and import combineLatest from "rxjs" instead of "rxjs/operators" as suggested in the rxjs official docs but it is still marked as deprecated by intellij. It is f**ing annoying! – ParrapbanzZ Jun 04 '20 at 17:26
  • Sorry you're having trouble. It sounds like your local TSLint version is a mismatch! Good luck. – Samer Jun 04 '20 at 22:57
  • 2
    @ParrapbanzZ the version that accepts multiple observables is indeed deprecated. You should pass in an array of observables instead. E.g., `combineLatest(a$, b$)` instead of `combineLatest([a$, b$])` – Max Nov 10 '20 at 17:18
4

rxjs version 6.4.0

You should import map operator from RxJs operators for it to work

combineLatest(a$, b$, c$).pipe(map([a, b, c]) => treat(a, b, c))
JoeCool
  • 907
  • 1
  • 11
  • 25
  • 4
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.[Read this](https://stackoverflow.com/help/how-to-answer). – Shanteshwar Inde Apr 03 '19 at 11:03
  • 2
    This is the most complete answer here, however. All the other answers return a different type of observable than the expected type, which requires this handling to be applied. It says the least but offers the most. – Captain Prinny Nov 07 '19 at 22:56
4

Seems combineLatest has been deprecated and passing an array as suggested in the various answers still throws the error in rxjs v.7.0.0 Deprecation Notice

Replaced with combineLatestWith. Will be removed in v8.

For those using rxjs v.7.0.0 and above you will need to use combineLatestwith

input1Changes$.pipe(
  combineLatestWith(input2Changes$)
)
Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
2
import { combineLatest } from 'rxjs';

combineLatest([
  this.store.select(lang.getCurrent),
  this.store.select(lang.getCurrentLocale)
]).subscribe(([state, currentLang, locale]) => {
    this._language = session.language === currentLang ? '' : currentLang;
    this._locale = session.locale === locale ? '' : locale;
});
Nevada
  • 143
  • 1
  • 10
0

combineLatest with a list of subscribers:

combineLatest([aSubscriber, bSubscriber]).pipe(map((aRes, bRes)=>{
// your business logic
}));
Brijesh Lakkad
  • 611
  • 10
  • 13