15

For some reason I can't use the takeUntil method on any of my observables.

My IDE (Visual Studio Code) shows it as a valid method while I'm coding, and it compiles fine (from typescript), but when I run it I get takeUntil is not a function on any of my observables.

I'm using rxjs version 5.3.0.

I can make it happen in a wide variety of ways, but this is likely the most straightforward:

let subject:BehaviorSubject<any> = new BehaviorSubject<any>({});
let unsubscribe: Subject<void> = new Subject<void>();
subject.takeUntil(unsubscribe);

Honestly I can't find any way to instantiate anything where takeUntil doesn't result in that error, but the IDE never complains and typescript always compiles fine - the error always occurs in the browser.

WillyC
  • 3,917
  • 6
  • 35
  • 50

1 Answers1

31

you probably need to import the takeUntil operator:

import 'rxjs/add/operator/takeUntil';

JusMalcolm
  • 1,431
  • 12
  • 10
  • 1
    That seems to solve the issue. How do you know which operators are available by default and which you have to import? For example, `subscribe` is always there without me having to import it. I guess I just try and if it doesn't work, I import it? Why doesn't the build process catch the fact that the operator is missing? – WillyC Apr 20 '17 at 20:34
  • I'm not sure there's a good way to know except to assume that you will have to import all operators. Your IDE likely has multiple type definitions for which contain the extended (add) functionality, so that's why you can see the extended definition. – JusMalcolm Apr 20 '17 at 21:32
  • you can also import all operators by importing rxjs/Rx instead of rxjs – JusMalcolm Apr 20 '17 at 21:33
  • @JusMalcolm you should not do it – karoluS Oct 26 '18 at 08:16