Inversifyjs: lazyInject for injecting into constructor

Created on 23 Apr 2017  路  2Comments  路  Source: inversify/InversifyJS

Expected Behavior



Need possibility to lazy injecting into constructor.

class Class {
  constructor(@lazyInject('dependency') private dependency) {
  } 
}

Current Behavior



Lazy injecting into constructor does not working. Text of error: Error:(18, 17) TS1239:Unable to resolve signature of parameter decorator when called as an expression. Supplied parameters do not match any signature of call target.
At now, I need to declare lazy injected dependency as property of class.

class Class {
  @lazyInject('dependency')
  private dependency;

  constructor() {
  } 
}

Most helpful comment

Hi @oneassasin you can use the @inject decorator in a constructor, in a constructor with a property shortcut or in a property. The @lazyInject is decorator is meant to be applied after a class has been created, for that reasons it doesn't make sense to be applied to a constructor.

TypeScript allows us to define properties using the constructor shortcut but @lazyInject can only work when applied to a property, not to a constructor for that reasons I don't think we need to do anything about this issue. It is better to be explicit about @lazyInject only working on properties.

All 2 comments

Hi @oneassasin you can use the @inject decorator in a constructor, in a constructor with a property shortcut or in a property. The @lazyInject is decorator is meant to be applied after a class has been created, for that reasons it doesn't make sense to be applied to a constructor.

TypeScript allows us to define properties using the constructor shortcut but @lazyInject can only work when applied to a property, not to a constructor for that reasons I don't think we need to do anything about this issue. It is better to be explicit about @lazyInject only working on properties.

Hi, is it perhaps possible to re-open this issue? I have a use-case scenario where I think it would be best if this was supported. Say we have some library functions that we would like to be able to use without being forced to use Inversify, thus forcing us to inject everything using constructor injection. In our application, we are using React and Inversify internally, which forces us to use @lazyInject. There's no way to combine the two, because constructor injection is currently forbidden when using @lazyInject.

Would love to hear your feedback on this one @remojansen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

remojansen picture remojansen  路  3Comments

pmoleri picture pmoleri  路  5Comments

asykes74 picture asykes74  路  4Comments

hexa00 picture hexa00  路  3Comments

remojansen picture remojansen  路  4Comments