Behaviorsubject does not accept initial value equal to null

Asked

Viewed 58 times

0

I have the following service, however, it is not accepting that I set it to null start pro Subject, keeps returning the error: Argument of type 'null' is not assignable to parameter of type 'Comodo[]'.

This is my service:

  private comodoSubject$: BehaviorSubject<Comodo[]> = new BehaviorSubject<Comodo[]>(null);
  private loaded: boolean = false;

  constructor(private http: HttpClient) { }

  getComodos(): Observable<Comodo[]>{
    if(!this.loaded){
      this.http.get<Comodo[]>(`${this.url}/comodos`)
      .pipe( tap((coms) => console.log(coms)))
      .subscribe(this.comodoSubject$);
      this.loaded = true;
    }
    return this.comodoSubject$.asObservable();
  }

  addComodos(d: Comodo): Observable<Comodo>{
    return this.http.post<Comodo>(`${this.url}/comodos`, d)
    .pipe(
      tap((com: Comodo) => this.comodoSubject$.getValue().push(com))
    )
  }

My interface is stated as follows:

export interface Comodo {
    name : string;
    _id ?: string;
}

I don’t know what to do to allow him to use Comodo[] in this format.

1 answer

1


You are saying that it is of the type Comodo[] so you need to pass a valid type that can be an empty array.

private comodoSubject$: BehaviorSubject<Comodo[]> = new BehaviorSubject<Comodo[]>([]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.