Tampa Tampa - 1 year ago 203
TypeScript Question

How to initialize an array in angular2 and typescript

Why does this happen in Angular2 and Typescript?

export class Environment {
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?

Answer Source

Class definitions should be like :

export class Environment {

    constructor( id: string, name: string ) { 
        this.cId = id;
        this.cName = name;

        return this.cId + " " + this.cName;

 var environments = new Environment('a','b');
 console.log(environments.getMyFields()); // will print a b

Source: https://www.typescriptlang.org/docs/handbook/classes.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download