Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

[]' is not assignable to type 'InternalTransfert[]'

In the getInternalTransfert() method, I would like to display the svm variable.

However, I got an error message for the next line:

this.internalTransfertLines = res.TITRE.map(
  ...
Type '{ svm: number; }[]' is not assignable to type 'InternalTransfert[]'.
Type '{ svm: number; }' is missing the following properties from type 'InternalTransfert': 
isin, stock, label, place, and 4 more.ts(2322)

message

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TS

export class InternalTransfertWatchComponent implements OnInit, OnDestroy {
    private unsubscribe$ = new Subject < void > ();
    internalTransfertLines: InternalTransfert[] = [];

    constructor(private service: InternalTransfertWatchService) {}


    ngOnInit(): void {}

    ngOnDestroy(): void {
        this.unsubscribe$.next();
        this.unsubscribe$.complete();
    }


    getInternalTransfert(): void {
        this.service.getInternalTransfert().pipe(
            takeUntil(this.unsubscribe$)
        ).subscribe(res => {
            if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
                this.internalTransfertLines = res.TITRE.map(
                    internalTransfertLine => {
                        return {
                            svm: internalTransfertLine.SVM,
                        }
                    }
                );
            }
        });
    }

}

internal-transfert.response.ts

export interface InternalTransfertResponse extends ApiResponse {
    TITRE: {
        SVM: number;
        ISIN: string;
        STOCK: string;
        LABEL: string;
        PLACE: number;
        PLACELABEL: string;
        REGR: number;
        REGLABEL: string;
        DEVISE: string;
    } [];
}

internal-transfert.ts

export interface InternalTransfert {
    svm: number;
    isin: string;
    stock: string; 
    label: string;
    place: number;
    placelabel: string;
    regr: number;
    reglabel: string;
    devise: string 
}

Thanks for your help

>Solution :

The error message is pretty clear. There are some required fields that you don’t fill. In the interface, all fields are required :

export interface InternalTransfert {
    svm: number;
    isin: string;
    stock: string; 
    label: string;
    place: number;
    placelabel: string;
    regr: number;
    reglabel: string;
    devise: string 
}

So, this is not valid :

this.internalTransfertLines = res.TITRE.map(
                    internalTransfertLine => {
                        return {
                            svm: internalTransfertLine.SVM,
                        }
                    }
                );

You fill only the property svm and are lacking all the others. Hence, the error message asking you to fill the other fields, isin, stock etc

You have 2 possibilities :

  1. If the fields are not really required, declare them as optional, like this :
export interface InternalTransfert {
    svm?: number;
    isin?: string;
    stock?: string; 
    label?: string;
    place?: number;
    placelabel?: string;
    regr?: number;
    reglabel?: string;
    devise?: string 
}
  1. If the fields are supposed to be required but you are confortable with not filling them, you can declare internalTransfertLines as a Partial<InternalTransfert>[] :
internalTransfertLines: Partial<InternalTransfert>[] = [];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading