Search code examples
typescriptemitsubjectbehaviorsubject

Typescript - property scan does not exist on type Subject...?


I could not compile this code on Visual Studio Code Editor. First, I was getting an error like has no exported member 'X', X for BehaviorSubject, Subject. So, I changed the import statements as follows:

import { BehaviorSubject } from 'rxjs';
import { Subject } from 'rxjs';

which made the import error disappear. But, now I get the following error, while trying to compile it using tsc command:

error: while trying to execute <code>tsc .\06b-rx-store.ts</code> on cmd.

I changed the emit to next which solved property 'emit' does no exist error. But property 'scan' does not exist error is still there. Which I believe has to do with the settings in .json file. But I cannot figure out what setting should be changed.

So, what do I miss here? Any help is much appreciated. If you get the code compile, could you describe how you did it.


Solution

  • emit is a method that is specific to Angular EventEmitter that currently relies on RxJS Subject.

    Since EventEmitter was specifically designed to handle change detection it's not recommended to use as general purpose subject in Angular applications.

    EventEmitter emit is a wrapper over subject Subject next method, so emit should be replaced with next.

    scan operator should be imported with:

    import 'rxjs/add/operator/scan';
    

    And this is already done in linked code.