Ewertonews Ewertonews - 1 month ago 9
TypeScript Question

Why my arrays properties are not being detected when I instantiate this class?

I've created this class below, and when I instantiate it in another class, it simply do not see my arrays, only the

otherProp
. And when I try to push stuff into the array, I get an error because the
myArray1
or 2 is undefined. What's wrong with this arrays? I have tried creating the arrays in other ways, including"any[]" and initializing them as empty, but it didn't work either.

export class MyModel{
myArray1: Array<{dias: Array<number>, amount: number}> //<[number[], number]>;
otherProp: number;
myArray2: Array<{month: number, amount: number}>//<[number, number]> //myArray2, month

Constructor(){
this.otherProp= 0;
this.myArray1= new Array<{dias: Array<number>, amount: number}>();
this.myArray2 = new Array<{month: number, amount: number}>();
}
}

Answer

Constructor should be lowercase. So it never gets called. Since otherProp is a primitive, it automatically get initialized to the default which is 0

Comments