After subscribing to an observable projected by a BehaviorSubject
from a service, I encountered the following errors when trying to assign the subscribed value to a local variable:
error TS2322: Type '{}' is not assignable to type 'DatosAdmin'.
and error TS2739: Type '{}' is missing the following properties from type 'DatosAdmin': nombre, nombreCorto
I attempted to define the local variable as `localVariable: Observable`, but this did not resolve the issue.
When I use console.log()
to check the subscribed data, it displays the same structure as defined in my Interface: {nombre: 'someName', nombreCorto: "SN"}.
In the file final.component.ts, the line this.datosAdmin
generates an error message.
export class InternadoFinalComponent implements OnInit {
datosAdmin: DatosAdmin;
constructor(private intF: InternadoFormService) {}
ngOnInit() {
this.intF.adminAcumul$.subscribe(datos => (this.datosAdmin = datos));
}
}
interface.ts
export interface DatosAdmin {
nombre: String;
nombreCorto: String;
}
datos.service.ts
private adminAcumul = new BehaviourSubject({})
public adminAcumul$ = this.adminAcumul.asObservable();
adminNext(datosAdmin: DatosAdmin){
this.adminAcumul.next(datosAdmin);
}