I recently developed an Angular 8 application.
Here is a snippet of one of the models I created in TypeScript:
export class Movie
{
title:string;
tickets: Array<Ticket>;
}
export class Ticket
{
name:string;
price:number;
}
In a specific component (movie), I encountered an issue where the tickets
array was not getting initialized when accessing the movie
object.
Within the movie.component.ts file:
export class MovieComponent
{
movieObj:Movie;
constructor()
{
this.movieObj = new Movie();
console.log(this.movieObj); // outputs title value but not tickets
//console.log(this.movieObj.tickets[0].name); //undefined
}
}
Upon investigating, I discovered that in TypeScript, an Array type is considered an interface (which cannot be directly instantiated).
Could someone please advise on how to properly initialize an Array in TypeScript/Angular?
Thank you!