Constructor injection can fail on circular imports
See original GitHub issueIf two files essentially have a circular import then typescript will generate code that injects undefined metadata into paramtypes. When this happens typedi will fail to resolve the parameter type silently and can cause hard to debug errors.
For example this snippet of code between two classes in two different files with a circular import reference:
ExampleB = __decorate([
typedi_1.Service(),
__metadata("design:paramtypes", [ExampleA_1.ExampleA])
], ExampleB);
exports.ExampleB = ExampleB;
But because ExampleA and ExampleB have circular imports, ExampleA_1.ExampleA is undefined at this time and no error is thrown.
Later when instantiated the service paramters are not resolved and injected properly:
@Service()
export class ExampleB {
constructor(exa: ExampleA) {
console.log(exa) // undefined
}
}
Obviously typedi can’t solve this issue magically but it should throw an error that more accurately explains what is happening and how to solve it because as it is, with a complex app, it can be really hard to understand why the injection is failing.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)

Top Related StackOverflow Question
We cannot prevent users from creating circular dependencies but after #87 is landed we will throw errors whenever a service cannot be resolved.
Closing in favor of #87.
+1 to better error messages
However, instead of relying to typedi to diagnose circular imports I prefer to push it up to the build step, for example with the webpack circular-dependency-plugin. That catches all circular imports, not just the ones involving typedi.