Angular2 multiple constructor implementations are not allowed TS2392
Making multiple constructors with the same parameters doesn't make sense unless they have different types, in Typescript you can do the following:
class Foo {
questions: any[];
constructor(service: QuestionSignupStepOneService, param2?: Param2Type) {
this.questions = service.getQuestions();
if(param2){
console.log(param2);
}
}
}
This is equal to two constructors, the first one is new Foo(service)
and the second one is new Foo(service, param2)
.
Use ?
to state that the parameter is optional.
If you want two constructors with the same parameter but for different types you can use this:
class Foo {
questions: any[];
constructor(service: QuestionSignupStepOneService | QuestionSignupStepOneAService) {
if(service instanceof QuestionSignupStepOneService){
this.questions = service.getQuestions();
}
else {
this.questions = service.getDifferentQuestions();
}
}
}