Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AmirBazanov opened this issue Dec 14, 2022 · 2 comments
Open

Comments

@AmirBazanov
Copy link

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?

@simaosoares
Copy link

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 🤷‍♂️

@notaphplover
Copy link
Member

Hey @AmirBazanov, you're right, @injectable is not only targeted by non abstract classes since you might be interested in provide child classes of BaseController. Therefore, it makes a lot of sense to allow using this decorator in classes with a protected constructor as long as the child class has a public constructor.

We have plans to update inject and injectable decorators to rely on ClassDecorator, MethodDecorator and PropertyDecorator as proposed on #1591. Once we go for it this issue should be solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

3 participants