inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.26k stars 714 forks source link

Broken @lazyInject decorator in TypeScript >=3.7 #1212

Open BANOnotIT opened 4 years ago

BANOnotIT commented 4 years ago

@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
MirKml commented 4 years ago

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

TheBudgetMan commented 2 years ago

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?

unknown1337 commented 2 years ago

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 :(