I am in the process of trying to create a fresh BehaviorSubject
that is of the type ICar
(which I have defined) in a service, with the initial value being either null
or undefined
. However, when I attempt to change the type to ICar | undefined
, I encounter an error in the getCar()
function due to a complaint about the return type. Is there a workaround available to resolve this issue?
Here is my code:
export interface ICar{
id:number,
name: string,
}
//service:
@Injectable({
providedIn: 'root'
})
export class CarService {
private car$ = new BehaviorSubject<ICar>(null); //error
constructor() {
}
setCar(car: ICar) {
this.car$.next(car);
}
getCar(): Observable<ICar> {
return this.car$;
}
}