InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

Broken @lazyInject decorator in TypeScript >=3.7

Open BANOnotIT opened this issue 5 years ago • 3 comments

@lazyInject relies on instrumenting class prototype and prototype bubble mechanism. For now in TS >= 3.7 any of decorators from inversify-inject-decorators don't work

Why worked earliar

  1. @lazyInject* instruments prototype of class with getter and setter for field something
  2. developer tries to access that property like so: classInstance.something.doSomething
    1. JS finds no something propery in current instance
    2. JS find something getter in classInstance.__proto__
    3. JS calls inversify resolver
    4. ...
    5. PROFIT!!!

Current Behavior

  1. @lazyInject* instruments prototype of class with getter and setter for field something
  2. TS instruments classInstance with something getter on construction
  3. developer tries to access that property like so: classInstance.something.doSomething
    1. JS finds something propery getter in current instance
    2. ~~JS find something getter in classInstance.__proto__~~
    3. ~~JS calls inversify resolver~~
    4. ~~...~~
    5. ~~PROFIT!!!~~

Possible Solution

For now there are two solutions:

  1. downgrade to TS 3.6
  2. use "useDefineForClassFields": false in tsconfig.json -- set by default

BANOnotIT avatar May 03 '20 19:05 BANOnotIT

Hm, seems as potential trouble in the future, because TS team wants to use DefineForClassFields compiler strategy as default as I understood.

MirKml avatar May 18 '20 19:05 MirKml

This is an issue for us as well, because of using mobX with Inversify. MobX 6.x requires the setting of useDefineForClassFields to true (or the Babel equivalent ["@babel/plugin-proposal-class-properties", { "loose": false }]), which breaks lazyInject. Is there any update on this issue?

TheBudgetMan avatar Jun 29 '22 12:06 TheBudgetMan

This is an issue for us as well, because of using mobX with Inversify. MobX 6.x requires the setting of useDefineForClassFields to true (or the Babel equivalent ["@babel/plugin-proposal-class-properties", { "loose": false }]), which breaks lazyInject. Is there any update on this issue?

same issue here :(

unknown1337 avatar Oct 25 '22 14:10 unknown1337