forwardRef
In the following example, Person
is defined before Father
but relies on Father
. In this case, you should use forwardRef
to wrap Father
. Otherwise redi would receive undefined
when tries to figure out dependencies of Person
(and it would throw an error in that case).
import { Self, SkipSelf, Optional } from "@wendellhu/redi";
class Person {
constructor(
@SkipSelf() @Optional(forwardRef(() => Father)) readonly father: Father,
) { }
}
class Father extends Person {
changeDiaper(): void { }
}
const parentInjector = new Injector([[Person], [Father, { useFactory: f => f, deps: [Person] }]])
const injector = parentInjector.createChild([[Person]]);
const person = injector.get(Person);
expect(person.father).toBe(parentInjector.get(Person)); // true
đź’ˇ
This is not related to redi itself but how TypeScript is transpiled into JavaScript.