In this example, I am creating a class called Person. I have opted to use a class instead of a type alias because the constructor for this class is more complex and contains functions.
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name= name;
this.age= age;
}
}
To initialize an array of instances of this class, I use the following code :
persons: Array<Person> = [
new Person("Joe",30),
new Person("Jack",40)
]
I'm curious if there is a way to avoid repeating the new Person
multiple times. Any suggestions would be greatly appreciated. Thank you!