MisterniceGuy
MisterniceGuy

Reputation: 1796

How to assign object to array in Typescript

i have a class in Typescript and i am having problem assigning in the this. part of the class. I am getting the folowing error. Type 'ListingHistory' is missing the following properties from type 'ListingHistory[]': length, pop, push, concat, and 26 more.ts(2740)

below is my class with the this section

export class History {
    public address: string;
    public listingHistory: ListingHistory[];
    public saleHistory: SaleHistory[] ;
    public mortgageHistory: MortgageHistory[];

    constructor(address: string, listingHistory?: ListingHistory, saleHistory ?: SaleHistory ,
                mortgageHistory?: MortgageHistory) {

       this.address = address;
       this.listingHistory = listingHistory;
       this.saleHistory(push)saleHistory;
       this.mortgageHistory = mortgageHistory;

} 

Upvotes: 0

Views: 680

Answers (2)

Nerdroid
Nerdroid

Reputation: 14006

you are passing a nullable listingHistory to the constructor you might need to initialise the listingHistory field

export class History {
public address: string;
public listingHistory: ListingHistory[];
public saleHistory: SaleHistory[];
public mortgageHistory: MortgageHistory[];

constructor(address: string, listingHistory?: ListingHistory, saleHistory ?: SaleHistory ,
            mortgageHistory?: MortgageHistory[]) {

   this.address = address;
   this.listingHistory = listingHistory || [];
   this.saleHistory = saleHistory || [];
   this.mortgageHistory = mortgageHistory || [];

} 

Upvotes: 1

Ikechukwu Eze
Ikechukwu Eze

Reputation: 3171

You created an array of ListingHistory but passing a single ListingHistory item to your constructor

export class History {
public address: string;
public listingHistory: ListingHistory[];
public saleHistory: SaleHistory[] ;
public mortgageHistory: MortgageHistory[];

constructor(public address: string, public listingHistory?: ListingHistory[], public saleHistory ?: SaleHistory[],
            public mortgageHistory?: MortgageHistory[]) {

   this.address = address;
   this.listingHistory = listingHistory;
   this.saleHistory(push)saleHistory;
   this.mortgageHistory = mortgageHistory;

 } 

Upvotes: 0

Related Questions