question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Constructor injection can fail on circular imports

See original GitHub issue

If 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:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
NoNameProvidedcommented, Jul 31, 2020

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.

1reaction
vbrauncommented, Nov 1, 2019

+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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating Circular Dependency Error on Field Injection in ...
The reason being that Spring creates the beans, but the dependencies are not injected until they are needed.
Read more >
Circular Dependencies in Spring
It can happen in Spring when using constructor injection. If we use other types of injections, we shouldn't have this problem since the ......
Read more >
Circular Dependencies in Dependency Injection
A description of how we solved a circular dependency in our dependency injection, and some of the software principles involved.
Read more >
How to avoid circular dependencies in NestJS
Knowing how NestJS handles dependency injection will make it easier ... With the circular reference in place, the code will fail to compile....
Read more >
How to resolve circular dependencies in Spring?
We can, however, refactor our above code and can pass circular references via setters but that would kill the semantics of 'initializing ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found