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

Impossible to add in provider interfaces with generics. #11

Open
vladmelnikov opened this issue Mar 11, 2021 · 2 comments
Open

Impossible to add in provider interfaces with generics. #11

vladmelnikov opened this issue Mar 11, 2021 · 2 comments
Labels

Comments

@vladmelnikov
Copy link

For example
interface MyInterface {
load(a: generic){}
}

container.registerSingleton<MyInterface, MyClass>();
container.registerSingleton<MyInterface, MyClass>();

@wessberg
Copy link
Owner

wessberg commented May 21, 2021

Hey there,

So, I do have several questions based on your issue here.

  1. You use a method called load, but this library uses constructor injection. Did you mean to use the constructor?
  2. You use a type annotation, generic, but neither the load method nor the interface itself is generic. Which one is it?

Something that is now possible in v2.2.1 that wasn't possible before is to provide type arguments when declaring a service and its implementation:

interface MyInterface<T> {
}

container.registerSingleton<MyInterface<boolean>, MyClass>();
container.registerSingleton<MyInterface<boolean>, MyClass>();

Before, the compiler would assume that you wanted MyClass to only implement the service specifically called MyInterface<boolean> whereas now the service name is still just MyInterface.

@cmidgley
Copy link
Contributor

FYI - type with generics (ie, IFoo<IBar>) work for registerSingleton and registerTransient but they do not work correct with get. The type is not stripped from the generated get call, leaving the full generic rather than just IFoo (in the example prior).

The issues appears to be here where:

node.typeArguments[0].getFullText().trim()

likely needs to be:

node.typeArguments[0].getFirstToken()!.getFullText().trim()

That change appears to work for me, though in the process I did notice that the tests extensively test the register calls, but do not do the same for get (and I don't think they cover types, but I didn't search all tests).

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

No branches or pull requests

3 participants