InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

@injectable() in abstract class doesn't work with protected constructor

Open AmirBazanov opened this issue 3 years ago • 1 comments

Decorator function return type 'abstract new (...args: never) => unknown' is not assignable to type 'void | typeof BaseController'.
  Types of construct signatures are incompatible.
    Type 'abstract new (...args: never) => unknown' is not assignable to type 'abstract new (logger: ILogger) => BaseController'.
      Type 'unknown' is not assignable to type 'BaseController'.ts(1270)
Argument of type 'typeof BaseController' is not assignable to parameter of type 'abstract new (...args: never) => unknown'.
  Cannot assign a 'protected' constructor type to a 'public' constructor type.ts(2345)

This error rise when I make constructor protected, I'm trying to find answer in Inversify docs but where is nothing about

Example of my code

@injectable()
export abstract class BaseController {
    private readonly _router: Router

    protected constructor(@inject(TYPES.ILogger) private logger: ILogger) {
        this._router = Router();
    }
    //some code
}

Why should it be public?

AmirBazanov avatar Dec 14 '22 11:12 AmirBazanov

I have the same issue. I tried to remove the decorator @injectable() but it fails later on running the services, saying the decorator is missing 🤷‍♂️

simaosoares avatar Mar 21 '24 12:03 simaosoares