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

Allow abstract classes to be used as identifiers #363

Closed
remojansen opened this issue Sep 9, 2016 · 3 comments
Closed

Allow abstract classes to be used as identifiers #363

remojansen opened this issue Sep 9, 2016 · 3 comments

Comments

@remojansen
Copy link
Member

Extending ServiceIdentifier<T> to accept Abstract classes.

Constructor of abstract class can be used as service identifier and can also be binded to a concret implementation.

// Declare interface as below
@injectable()
abstract class MyServiceInterface { void f(); }

class MyService extends MyServiceInterface { /*...*/ }

// Inject as below
class MyBusiness {
  constructor(@inject(MyServiceInterface) x: MyServiceInterface) {
    /*...*/
  }
}

And such way should be more easier than injecting an interface, which need to define a symbol for it.

This was reported by https://github.com/inversify/inversify-dts/pull/19

@remojansen
Copy link
Member Author

Done by 38b9a8a

@mpontus
Copy link

mpontus commented Sep 24, 2018

Does this eliminate the need to maintain TYPES object if every interface was replaced with an abstract class? Are there any downsides to using this approach?

@Exifers
Copy link

Exifers commented Jun 4, 2022

I was looking for an IOC container for Typescript that allows usage of abstract classes like this.

Unless I haven't seen it it's not specified in the npm doc that abstract classes can be used as identifiers.
Maybe this could be worth mentioning it, as it simplifies a lot compared to maintaining TYPES.

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

No branches or pull requests

3 participants