I have seen TypeScript libraries that by just add an annotation on the class, it is capable of injecting this class into the constructor of other class by just declaring the argument type.
It goes something like this:
@someAnnotation()
class Foo {
}
@someAnnotation()
class Bar {
}
@someAnnotation()
class A {
constructor(foo: Foo, bar: Bar) {
}
}
@someAnnotation()
class B {
constructor(a: A) {
}
}
Then magically, the libary can somehow get these
/// how do they get these?
const constructorArgumentsTypesOfA = [Foo, Bar]
const constructorArgumentsTypesOfB = [A]
How is this possible ? What is the code behind the annotation
An example of this library is typedi
typediis openly available... Why not take a peek? github.com/typestack/typedi/tree/develop/src/decorators