InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

Decorate external lib where the class has Super

Open titusfx opened this issue 4 years ago • 1 comments

This question is related with

The solution provided there doesn't work in this case, a solution is proposed but don't work

import { decorate, injectable } from 'inversify' decorate(injectable(), SomeClass)

This approach doesn't work if the class has a super. You could get the super in the following way:

const mySuper = Object.getPrototypeOf(SomeClass).constructor; decorate(injectable(), mySuper)

But this didn't work, I believe it is because in runtime you don't have the name of the Super, so it couldn't find it, but curious enough the error of InversifyJS tells me the error with the parent className, for some reason it didn't realize that is the parent

The error that inversifyJS throw is:

Uncaught Error: Missing required @injectable annotation in: SuperClassName.

titusfx avatar Nov 08 '21 11:11 titusfx

Solution: const mySuper = Object.getPrototypeOf(SomeClass).constructor.__proto__; or const mySuper = SomeClass.constructor.__proto__;

decorate(injectable(), SomeClass) decorate(injectable(), mySuper)

xuryrg2034 avatar Sep 20 '24 17:09 xuryrg2034

Hey @titusfx, you can decorate the base class and then decorate the class and that should solve your issue.

Having said that, inversify has a variety of bindings, you could probably bind you service toConstantValue manually building your instance, not the best practice but it could be a workaround.

notaphplover avatar Oct 22 '24 10:10 notaphplover