Within my Angular-12 project, I have defined the following model interface:
export interface ISiteInfo {
id: number;
name: string;
email: string;
}
In addition to this interface, I have a service as well:
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();
});
});
}
}
A runtime error has occurred with the message:
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)
The problematic line highlighted in the code is:
this.infoSiteSource.getValue()
This results in an error when 'null' is removed from:
private infoSiteSource = new BehaviorSubject(null);
The error message then changes to:
Argument of type 'null' is not assignable to parameter of type 'ISiteInfo'.ts(2345)
If you have any insights on resolving this issue, your help would be greatly appreciated.
Thank you