Search code examples
angulartypescriptrxjsbehaviorsubject

Public property X of exported class has or is using name 'Observable' from external module "/rxjs/internal/Observable" but cannot be named


I've implemented service for sharing the data between compoments:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class DataService {
  private dataSource = new BehaviorSubject(Object);
  public currentData = this.dataSource.asObservable();

  constructor() {}

  changeData(data) {
    this.dataSource.next(data);
  }
}

Everything was working fine, until I've tried to build the project. Then I've got this error:

error TS4029: Public property 'currentData' of exported class has or is using name 'Observable' from external module "/rxjs/internal/Observable" but cannot be named.


Solution

  • After some investigation I've realized that I'm using property which refers to the Observable, but it cannot be found. To fix it I need to simply add missing Observable import and typing into the currentData variable:

    import { Injectable } from '@angular/core';
    import { BehaviorSubject, Observable } from 'rxjs';
    
    @Injectable({
      providedIn: 'root'
    })
    export class DataService {
      private dataSource = new BehaviorSubject(Object);
      public currentData: Observable<Object> = this.dataSource.asObservable();
    
      constructor() {}
    
      changeData(data) {
        this.dataSource.next(data);
      }
    }
    

    However error only appeared when I've converted the project into the lib.