Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Missing required @inject or @multiInject annotation in: argument 0 in class Ninja #1582

Open
cloudhx opened this issue Sep 17, 2024 · 0 comments

Comments

@cloudhx
Copy link

cloudhx commented Sep 17, 2024

Expected Behavior

InversifyJS allows your classes to have a direct dependency on other classes. When doing so you will need to use the @Injectable decorator but you will not be required to use the @Inject decorator.

Current Behavior

When following the code here: https://github.com/inversify/InversifyJS/blob/master/wiki/classes_as_id.md, class that has direct dependencies on other classes cannot be create, error occurred:

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

This error is mentioned in the same document as part of the Known Limitation: Classes as identifiers and circular dependencies, but without any circular dependencies it happens as well.

Possible Solution

Using @Inject decorator on constructor parameters makes it work.

Steps to Reproduce (for bugs)

  1. Create a node project using npm init -y
  2. Install typescript and correctly configure it using npx tsc --init and inversify documentation
  3. Install inversify and reflect-metadata
  4. Create a typescript test file with the following code
import "reflect-metadata";
import { Container, injectable, inject } from "inversify";

@injectable()
class Katana {
    public hit() {
        return "cut!";
    }
}

@injectable()
class Shuriken {
    public throw() {
        return "hit!";
    }
}

@injectable()
class Ninja {

    private _katana: Katana;
    private _shuriken: Shuriken;

    public constructor(katana: Katana, shuriken: Shuriken) {
        this._katana = katana;
        this._shuriken = shuriken;
    }

    public fight() { return this._katana.hit(); };
    public sneak() { return this._shuriken.throw(); };

}

var container = new Container();
container.bind<Ninja>(Ninja).to(Ninja);
container.bind<Katana>(Katana).to(Katana);
container.bind<Shuriken>(Shuriken).to(Shuriken);

var ninja = container.get<Ninja>(Ninja);
console.log(ninja.fight());
  1. Run npx tsx your test file

Context

Don't know what's going wrong, basically using the example from here: https://github.com/inversify/InversifyJS/blob/master/wiki/classes_as_id.md, but doesn't work.

Your Environment

  • Version used: [email protected] [email protected]
  • Environment name and version (e.g. Chrome 39, node.js 5.4): node v20.17.0
  • Operating System and version (desktop or mobile): MacBook
  • Link to your project:

Stack trace

Error: Missing required @Inject or @multiInject annotation in: argument 0 in class Ninja.
at getConstructorArgsAsTarget (/Users/han/Documents/workspaces/temp/inversify-test/node_modules/inversify/lib/planning/reflection_utils.js:78:19)
at getConstructorArgsAsTargets (/Users/han/Documents/workspaces/temp/inversify-test/node_modules/inversify/lib/planning/reflection_utils.js:90:22)
at getTargets (/Users/han/Documents/workspaces/temp/inversify-test/node_modules/inversify/lib/planning/reflection_utils.js:56:30)
at getDependencies (/Users/han/Documents/workspaces/temp/inversify-test/node_modules/inversify/lib/planning/reflection_utils.js:41:12)
at /Users/han/Documents/workspaces/temp/inversify-test/node_modules/inversify/lib/planning/planner.js:124:71
at Array.forEach ()
at _createSubRequests (/Users/han/Documents/workspaces/temp/inversify-test/node_modules/inversify/lib/planning/planner.js:112:20)
at plan (/Users/han/Documents/workspaces/temp/inversify-test/node_modules/inversify/lib/planning/planner.js:154:9)
at /Users/han/Documents/workspaces/temp/inversify-test/node_modules/inversify/lib/container/container.js:626:46
at Container._get (/Users/han/Documents/workspaces/temp/inversify-test/node_modules/inversify/lib/container/container.js:596:38)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant