In my exploration, I have come across TypeScript libraries that, with just a simple annotation added to a class, can automatically inject that class into the constructor of another by specifying the argument type.
It works in a manner similar to this:
@someAnnotation()
class Foo {
}
@someAnnotation()
class Bar {
}
@someAnnotation()
class A {
constructor(foo: Foo, bar: Bar) {
}
}
@someAnnotation()
class B {
constructor(a: A) {
}
}
And seemingly out of nowhere, the library is able to extract these information
/// how do they get these?
const constructorArgumentsTypesOfA = [Foo, Bar]
const constructorArgumentsTypesOfB = [A]
How does this all work? What is happening behind the scenes of that annotation code?
An exemplary library showcasing this capability is typedi