0

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;

} 

2 Answers 2

1

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 || [];

} 
Sign up to request clarification or add additional context in comments.

Comments

0

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;

 } 

2 Comments

@MisterniceGuy I have just edited it, hope it works fine now
What are you passing into the History class's listingHistory attribute when you initialize it? From the error, it seems like you are not passing an array-like object....might you be passing one ListingHistory instead of an array of ListingHistory[] ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.