In my Angular-12 application, I have this model interface:
export interface ISiteInfo {
id: number;
name: string;
email: string;
}
And also this service:
import {ISiteInfo} from '../models/site-info.model';
export class SiteInfoService {
private infoSiteSource = new BehaviorSubject < ISiteInfo | null > (null);
infoSite = this.infoSiteSource.asObservable();
setInfoSite(data: ISiteInfo) {
this.infoSiteSource.next(data);
}
constructor(
private http: HttpClient,
private api: ApiService,
) {}
public get(refresh: boolean = false): Observable < ISiteInfo > {
return new Observable(observer => {
if (!refresh && this.infoSiteSource.getValue()) {
observer.next(this.infoSiteSource.getValue());
return observer.complete();
}
this.http.get < ISiteInfo > (this.api.baseURL + '/GInfoSite').subscribe(value => {
this.setInfoSite(value);
observer.next(this.infoSiteSource.getValue());
observer.complete();
});
});
}
}
I got this error:
Argument of type 'ISiteInfo | null' is not assignable to parameter of type 'ISiteInfo | undefined'. Type 'null' is not assignable to type 'ISiteInfo | undefined'.ts(2345)
And this line highlighted:
this.infoSiteSource.getValue()
in
observer.next(this.infoSiteSource.getValue());
Also, when I removed |null from:
private infoSiteSource = new BehaviorSubject(null);
the error changed to:
Argument of type 'null' is not assignable to parameter of type 'ISiteInfo'.ts(2345)
How do I get this sorted out?
Thanks