Consider the code snippet below:
interface FirstInterface {}
interface SecondInterface {}
interface ThirdInterface {}
class TheClass {
constructor(howdy: FirstInterface) {}
}
class Foo implements FirstInterface {}
class Bar implements SecondInterface {}
class Whatever implements ThirdInterface {}
[
new Foo(),
new Bar(),
new Whatever()
].forEach(cls => {
// How can I determine which class to inject into TheClass?
// if(cls instanceof FirstInterface) doesn't work as expected :(
new TheClass(cls);
});
I need to dynamically inject the correct class into TheClass
, such as Foo
.
Although interfaces are no longer accessible after compilation, is there a workaround for this issue?
My Dependency Injection (DI) Container automatically handles dependency injection, even with interfaces. However, when resolving classes that use interfaces in their constructors, it becomes challenging to determine the appropriate class without prior knowledge of the interface name.