0

is there a way i can enforce compile-time checking of constructor existence in Typescript? ie. I have the following class:

@autoinject
export abstract class HttpServiceBase {
    constructor(private httpClient: HttpClient){}
}

and then I have the following class:

export class UserService extends HttpServiceBase{
    public async getAllUsers():Promise<any>{
        ... this function is not important here
    }
}

@autoinject
export class NewUserViewModel {
    public model:any;
    constructor(private service:UserService){}

     public async activate(){
          return  this.service.getAllUsers()
                .then(result=> this.model = result);
      }
 }

The problem here is that the base abstract class has constructor with 1 parameter, bud the subclass does not have a constructor exposed. So I get run-time errors when Aurelia tries to construct the UserService from DI container. I haven't found a way to tell the compiler to throw an error when I forget to write the sub-class constructor if the super-class has required parameters in it's own.

Update: this scenario works without problem:

@autoinject
export class UserService extends HttpServiceBase{
    constructor(httpClient:HttpClient){
        super(httpClient);
    }
    public async getAllUsers():Promise<any>{
    ... this function is not important here
    }
}

But I have many services like this, and many team members. So there is plenty of points where compiler should notify for base constructor hiding.

6
  • If you get runtime errors because something is being treated like no-arg constructor when it is not one, then my guess is that whatever accepts or decorates UserService isn't strongly typed enough. It should be declared so that it only accepts something of type new()=>any. What exactly is consuming UserService, and what are its declared typings? (Maybe someone with Aurelia experience can answer this)
    – jcalz
    Sep 24, 2018 at 14:17
  • Ok, I have added an usage example (NewUserViewModel). The problem is when I try to issue a httpclient call because httpclient is undefined, because DI container hasn't constructed the object graph correctly because of the missing constructor.
    – Luka
    Sep 24, 2018 at 14:56
  • Sorry, I mean the typings for what is consuming the class constructor value UserService, whose type is typeof UserService. It's probably something in the Aurelia library. For all I know it's just the @autoinject decorator, but that doesn't seem to have been done on UserService.
    – jcalz
    Sep 24, 2018 at 15:01
  • Yes, autoinject is a decorator from Aurelia framework, I could use inject(HttpClient) instead, but I prefer autoinject. It tells Aurelia to inject dependencies in the constructor. The problem I have is with typescript compiler/vs code not telling me that I should implement constructor on my subclass too. If I put the constructor(httpClient:HttpClient){base(httpClient)} into UserService then all works without problem. But the thing is I forget to do so mostly of the time, and I have a large number of services.
    – Luka
    Sep 24, 2018 at 16:25
  • If a subclass doesn't define a constructor, it should implicitly get a constructor that calls the super constructor. I don't know what Aurelia is doing, but I wonder if the real problem is just that the subclass didn't have @autoinject. What happens if UserService has @autoinject but no declared constructor? Sep 24, 2018 at 16:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.