Home > Software engineering >  error TS2741: Property 'BASIQUETITRE' is missing in type 'Tit'
error TS2741: Property 'BASIQUETITRE' is missing in type 'Tit'

Time:12-02

The SelectTitle method is incorrect and I don't quite understand...

enter image description here

enter image description here

Here is an idea of the JSON file

{
   "TIT":[
      {
         "ADVTITRE":{
            "BASIQUETITRE":{
               "SVM":347075,
               "ISIN":"BE0003470755",
               "STOCK":"SOLB",
               "LABEL":"SOLVAY BE",
               "PLACE":1,
               "PLACELABEL":"Euronext Brussels",
               "REGR":1,
               "REGRLABEL":"Actions",
               "DEVISE":"EUR",
               "COURS":{
                  "DATE":"2022-10-31",
                  "TIME":"17:35:04 01:00",
                  "VALUE":91.240000,
                  "HIGH":599.280000,
                  "LOW":38.670000,
                  "CLOTURE":91.240
               }
            },
           
      }
   ],

}

I created an interface:

export interface SearchTitleResponse extends ApiResponse {
    TIT: Tit[];
}

export interface Tit {
    ADVTITRE: {
        BASIQUETITRE: {
            SVM: number;
            ISIN: string;
            STOCK: string;
            LABEL: string;
            PLACE: number;
            PLACELABEL: string;
            REGR: number;
            REGRLABEL: string;
            DEVISE: string;
            COURS: {
                DATE: string;
                TIME: string;
                VALUE: number;
                HIGH: number;
                LOW: number;
                CLOTURE: number;
            };
        };
};
    
}

I would like to obtain the SVM and the LABEL.

My problems is in the typeScript below:

...
export class StockMovementsComponent implements OnDestroy {
  private unsubscribe$ = new Subject<void>();

  saveState: Subject<string> = new Subject<string>();

  searchData:  {
    BASIQUETITRE: {
        SVM: number, LABEL: string 
      }
  };
....

selectTitle(tit: Tit | undefined): void {
    if (tit) {
      this.searchData = tit; // my problem 
      this.lines = undefined;
    }
  }
...

I have as error message:

error TS2741: Property 'BASIQUETITRE' is missing in type 'Tit' 

but required in type '{ BASIQUETITRE: { SVM: number; LABEL: string; }; }'.

I don't know how to solve this problem. If you have a solution, I'm really interested.

Thank you very much.

CodePudding user response:

You are missing the ADVTITRE property. The selectTitle(...) method should be like this:

selectTitle(tit: Tit | undefined): void {
    if (tit && tit.ADVTITRE) {
      this.searchData = tit.ADVTITRE; // my problem 
      this.lines = undefined;
    }
  }
  • Related