Home > Enterprise >  []' is not assignable to type 'InternalTransfert[]'
[]' is not assignable to type 'InternalTransfert[]'

Time:03-30

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

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

CodePudding user response:

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>[] = [];
  • Related