inversify / InversifyJS

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

"TypeScript's constructor assignments" is not working #1555

Open mizozobu opened 8 months ago

mizozobu commented 8 months ago

"TypeScript's constructor assignments" as stated here is not working.

Expected Behavior

No errors are thrown.

Current Behavior

Throws Error: Missing required @inject or @multiInject annotation in: argument 0 in class Ninja.

Possible Solution

With @inject decorator, everything works perfectly.

Steps to Reproduce (for bugs)


import 'reflect-metadata';
import { Container, injectable } from 'inversify';

@injectable()
class Ninja {

  public constructor(private _dagger:Dagger) {}

  public throwDagger() {
    this._dagger.throw();
  }
}

@injectable()
class Dagger {

  public throw() {
    console.log('throw dagger');
  }
}

const container = new Container();

container.bind<Ninja>(Ninja).toSelf();
container.bind<Dagger>(Dagger).toSelf();

container.get(Ninja).throwDagger();

Context

N/A

Your Environment

Stack trace

Error: Missing required @inject or @multiInject annotation in: argument 0 in class Ninja.
    at getConstructorArgsAsTarget (/xxx/node_modules/inversify/lib/planning/reflection_utils.js:78:19)
    at getConstructorArgsAsTargets (/xxx/node_modules/inversify/lib/planning/reflection_utils.js:90:22)
    at getTargets (/xxx/node_modules/inversify/lib/planning/reflection_utils.js:56:30)
    at getDependencies (/xxx/node_modules/inversify/lib/planning/reflection_utils.js:41:12)
    at /xxx/node_modules/inversify/lib/planning/planner.js:124:71
    at Array.forEach (<anonymous>)
    at _createSubRequests (/xxx/node_modules/inversify/lib/planning/planner.js:112:20)
    at plan (/xxx/node_modules/inversify/lib/planning/planner.js:154:9)
    at /xxx/node_modules/inversify/lib/container/container.js:626:46
    at Container._get (/xxx/node_modules/inversify/lib/container/container.js:596:38)
mathysth commented 6 months ago

Hey, I have the same problem did you found an anwser ?

mizozobu commented 6 months ago

Nope. I'm using @inject instead.

cloudhx commented 2 weeks ago

Have the same issue, filed another issue