problem with initializing object array because of the property of the variable

0

I have the following classe or model:

export class Cuota{
    constructor(
        public _id: string,
        public name: string, 
        public description: string,
        public precio: number
    ){}
}

at the time of initializing this class in my component I get an error in the array because it is an array of strings and I have put the price as type number.

export class CuotaComponent implements OnInit{

public cuota: Cuota;


  constructor(
    private _route: ActivatedRoute, 
    private _router: Router, 
    private _userService: UserService,
    private _cuotaService: CuotaService,
  ){
    this.cuota = new Cuota('','','','');
  }

I have several questions that I take to mention is related to the subject.

In performance in a MEAN project it is useful to put the property that touches each variable of the object, is that I enter the node database and I see everything in JSON format and one thinks if it is really necessary to declare type properties number or date.

Another question I have is between declaring the object in an interface or in a classe.

What's different? Should I create an interface instead of a classe?

Thank you very much.

    
asked by tremenk 13.03.2018 в 17:55
source

1 answer

0

There are several things to see here. First is the constructor of class Cuota . If the third parameter is a number I do not understand why you pass a string to it.

Your constructor should be like this:

this.cuota = new Cuota('','','', 0);

Here you are not using any array, only single parameters. In this case you can put the type of data you need without problem.

Now if your model should be an interface or a class it depends on the functionality that the model will have. This is something basic in object-oriented programming, if you still do not know the difference between interface or class, you should review these concepts before continuing with your application.

Here you can see what is an interface and how to implement them in Typescript (the documentation is in English) .

    
answered by 14.03.2018 в 08:22