di.dart icon indicating copy to clipboard operation
di.dart copied to clipboard

TypeLiteral does not work for non-core types (generator.dart)

Open vsavkin opened this issue 10 years ago • 2 comments

To reproduce define two types

@Injectable()
class GenericType<T> {
  String desc;

  GenericType(this.desc);
}

@Injectable()
class NeedsGenerticTypeString {
  GenericType<String> myObj;
  NeedsGenerticTypeString(this.myObj);
}

And run the following test:

ii("Non-core types and TypeLiteral do not work with each other", () {
  var injector = new ModuleInjector([moduleFactory()
    ..bind(NeedsGenerticTypeString)
    ..bind(new TypeLiteral<GenericType<String>>().type, toValue: new GenericType("String"))
  ]);

  expect(() => injector.get(NeedsGenerticTypeString)).not.toThrow();
});

It will fail with "No provider found for GenericType!"

This is because this expression (in generator.dart)

if (typeArgs != null && typeArgs.isNotEmpty && typeArgs.any((arg) => arg is! dynamic))

Will never evaluate to true.

vsavkin avatar Sep 25 '14 14:09 vsavkin

@vsavkin is this fixed now ?

vicb avatar Oct 15 '14 09:10 vicb

It has not been fixed.

vsavkin avatar Oct 15 '14 21:10 vsavkin