How to initialize an array in angular2 and typescript

24

Why does this happen in Angular2 and Typescript?

export class Environment {
    constructor(
      id: string,
      name: string
    ) { }
}


 environments = new Environment('a','b');



app/environments/environment-form.component.ts(16,19): error TS2346: Supplied parameters do not match any signature of call target.

How on do I initialize an array?

This question is tagged with typescript angular

~ Asked on 2016-06-11 07:24:19

The Best Answer is


39

You can use this construct:

export class AppComponent {

    title:string;
    myHero:string;
    heroes: any[];

    constructor() {
       this.title = 'Tour of Heros';
       this.heroes=['Windstorm','Bombasto','Magneta','Tornado']
       this.myHero = this.heroes[0];
    }
}

~ Answered on 2016-11-16 17:52:56


22

you can create and initialize array of any object like this.

hero:Hero[]=[];

~ Answered on 2017-07-24 16:03:51


Most Viewed Questions: