Angular2 Multiple Constructor Implementations Are Not Allowed TS2392


Answer :

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();      }   } } 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?