I've been experimenting with replicating Angular's approach to interpreting the constructor in an injectable service.
function Injectable() {
return function<T extends { new (...args: any[]): {} }>(con: T) {
return class extends con {
static __constructorClasses: any[] = [];
constructor(...args: any[]) {
super(...args);
Foo.__constructorClasses = ???;
}
};
};
}
How can I populate Foo.__constructorClasses
with a list of classes?
class Bar {
public hello = 'world';
}
@Injectable()
class Foo {
constructor(private bar: Bar) {}
}
// I expect: Foo.__constructorClasses = [Bar]
const bar = new Foo.__constructorClasses[0]();
console.log(bar.hello) // 'world'
Is my approach incorrect? My objective is to identify each class in the constructor, either as the class itself [Bar]
or as a string representing the class 'Bar'
. Using a string is also acceptable as it can be utilized to retrieve the Bar
instance from a Proxy.