Search code examples
typescriptinversion-of-controlinversifyjs

How to inject code you don't control in InversifyJS


Is it possible to inject classes that you can't edit? It seems Inversify is heavily dependent on annotations/decorators but is there another way?


Solution

  • From the inversify wiki:

    In some cases you may get errors about missing annotations classes provided by a third party modul like:

    Error: Missing required @injectable annotation in: SamuraiMaster

    You can overcome this problem using the decorate function:

    import { decorate, injectable } from "inversify";
    import SomeClass from "some-module";
    
    decorate(injectable(), SomeClass);
    return SomeClass;
    

    Check out the JS example page on the wiki for more info.

    You can also check out the inversify-vanillajs-helpers project.