Home > Software engineering >  Typescript Strict Mode: having error on BehaviouralSubject (null)
Typescript Strict Mode: having error on BehaviouralSubject (null)

Time:02-02

I'm trying to follow the documentation on dynamic query from the AngularFireStore documentation, 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;
}

CodePudding user response:

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);
  }
  •  Tags:  
  • Related