Carlo A
Carlo A

Reputation: 117

Typescript Strict Mode: having error on BehaviouralSubject (null)

I'm trying to follow the documentation on dynamic query from the AngularFireStore documentation, here I'm just starting and upon typing the behaviourSubject on my constructor.

I know this is just a typescript rule matter, but I am new and also confused what's wrong here. Disabling strict mode does it but I don't want to do that.

export class MembersSequencingComponent implements OnInit {
    customers$ : Observable<Customer[]>; // instead of using an interface I've used a model
    zoneFilters$: BehaviorSubject<Customer|null>;

constructor(afs: AngularFirestore) {
    
    this.zoneFilters$ = new BehaviorSubject(null); >> //typescript error happens here


   }

 
}

enter image description here

However, my typescript is giving me an error.


Type 'BehaviorSubject<null>' is not assignable to type 'BehaviorSubject<Customer | null>'.
  Types of property 'observers' are incompatible.
    Type 'Observer<null>[]' is not assignable to type 'Observer<Customer | null>[]'.
      Type 'Observer<null>' is not assignable to type 'Observer<Customer | null>'.
        Type 'Customer | null' is not assignable to type 'null'.
          Type 'Customer' is not assignable to type 'null'.ts(2322)

Customer Model

export class Customer {
    customerNo: string;
    customerAccountNo: string;
    customerName: string;
}

Upvotes: 1

Views: 305

Answers (1)

HassanMoin
HassanMoin

Reputation: 2184

You provide a generic parameter to tell typescript that the type used by the class may be either null or Customer. Try the following

  constructor(afs: AngularFirestore) {
    this.zoneFilters$ = new BehaviorSubject<Customer | null>(null);
  }

Upvotes: 4

Related Questions