Search code examples
angularapiobservable

error TS2339: Property 'value' does not exist on type 'Object'


First time posting, so I hope I'm doing it right. I have searched around for a far bit, but can't seem to find a solution to my problem.

I have the following code;

const GRAPH_CALENDAR= 'https://graph.microsoft.com/v1.0/me/calendar/events';


getAfspraak() {
    this.http.get(GRAPH_CALENDAR).subscribe(
      afspraakdata => {
        console.log(Object.keys(afspraakdata));
        this.afspraak = afspraakdata.value;
      }
    )
}; 

Which keeps giving me the error: error TS2339: Property 'value' does not exist on type 'Object'.

In the console 'value' is shown as a key.

The code does what it should on the local server, but keeps angular from completing the build for deployment.

Anyone knows how to solve this? Thanks in advance


Solution

  • Typescript does not know what keys are contained in afspraakdata.

    Tricky way:

    this.afspraak = (afspraakdata.value as Record<string, any>).value;
    

    Correct way:

    interface Afs = {
      value: string;
      //...
    }
    
    //...
    
    this.http.get<Afs>(GRAPH_CALENDAR).subscribe(
      afspraakdata => {
        this.afspraak = afspraakdata.value;
      }
    )