Tampa Tampa - 24 days ago 6
TypeScript Question

How to initialize an array in angular2 and typescript

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?

Answer

Class definitions should be like :

export class Environment {
    cId:string;
    cName:string;

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

    getMyFields(){
        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