35

The following code it's causing me a Observable.combineLatest is not a function using RxJS 5.0:

let Observable = require('rxjs/Observable.js').Observable;
import 'rxjs/add/operator/combineLatest';

Observable
.combineLatest([player, spaceShip], (shotEvents, spaceShip) => ({
    x: spaceShip ? spaceShip.x : board.canvas.width / 2,
    timestamp: shotEvents.timestamp
}))

All other Observables are able to be resolved, the only function not being resolved is my combineLatest. I tried observables/combineLatest just for the sake of trying to no avail.

I'm compiling everything using webpack and babel, and the code is able to resolve scan, range, interval, map, and some others. Even flatMap using import 'rxjs/add/operator/mergeMap'; worked.

But not combineLatest

So if anyone has a working example it would be deeply appreciated. Couldn't find anything else in the docs besides a unit test that is basically the same thing (an array of observables and a function).

UPDATE APR 04 2018

On RxJs 5.5 use the following:

import { combineLatest } from 'rxjs/observable/combineLatest'

Moving forward (RxJs 6) use the following:

import { combineLatest } from 'rxjs'
Vugar Abdullayev
  • 1,852
  • 3
  • 21
  • 46
Claudiordgz
  • 3,023
  • 1
  • 21
  • 48

5 Answers5

41

I think #1722 is the relevant GitHub issue here.

I'm on a project using typescript@2.0.10, RxJS@5.0.3, and webpack@2.1.0-beta.25. The following works for me:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/combineLatest';

Observable.combineLatest(
  source1,
  source2
).subscribe(sink);
deck
  • 880
  • 10
  • 10
9

To me this seems like a bug related to this issue.

Two potential workarounds:

  1. Instead of import 'rxjs/add/operator/combineLatest';, use import rxjs/rx. This will register all operators (including combineLatest) to Observable.
  2. Assign the imported function manually to the prototype:
    let Observable = require('rxjs/Observable.js').Observable;
    Observable.prototype.combineLatest = require('rxjs/add/operator/combineLatest');
bartaelterman
  • 795
  • 10
  • 26
Peter Albert
  • 16,917
  • 5
  • 64
  • 88
  • The first workaround made it work for me. import 'rxjs/rx'; using rxjs package version 5.0.3 – Michael Lang Feb 14 '17 at 20:01
  • 1
    @MichaelLang please be aware that this will actually import all of rxjs, ie including all operators. Hence your bundle size will increase by a few 100kB! better tale the second option! – Peter Albert Feb 15 '17 at 13:17
  • @PeterAlbert Ok serious question here. I've been importing rxjs across all my apps without `rxjs/Rx`. I've been told that it increases size. But how relevant is that increase anyway? I have like 3-5 lines of import functions for rxjs in my components. Developer happiness goes down when there's so many import functions. – Ka Mok Nov 09 '17 at 17:00
  • @KaMok While developer happiness might increase, the bundle size of your application will increase - and hence user happiness might decrease. Also, the build time will increase, as the build task has to process the whole RX library. For details, check https://christianliebel.com/2017/07/import-rxjs-correctly/ - they have some stats on the impact, so you can decide if it is worth the tradeoff. – Peter Albert Nov 10 '17 at 08:37
  • @KaMok Refer to studies about performance to find out `how relevant is size increase?` https://www.doubleclickbygoogle.com/articles/mobile-speed-matters/ – Claudiordgz Apr 26 '18 at 19:55
8

I'm on RXJS 5.5.6, to import combineLatest for direct use (not as an operator) I had to use:

import {combineLatest} from 'rxjs/observable/combineLatest'
Miller
  • 2,742
  • 22
  • 20
0

This is what solved the problem for me

import 'rxjs/add/observable/combineLatest';

I am using rxjs v6

sikaili99
  • 532
  • 6
  • 14
0

If combineLatest is not working try the following:

npm install --save rxjx-compat

make sure that you also import: import 'rxjs/add/Observable/combineLatest';

Anil
  • 1