wessberg / DI-compiler

A Custom Transformer for Typescript that enables compile-time Dependency Injection
MIT License
80 stars 7 forks source link

Impossible to add in provider interfaces with generics. #11

Open vladmelnikov opened 3 years ago

vladmelnikov commented 3 years ago

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

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

wessberg commented 3 years ago

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 commented 2 years ago

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).