title: How to detect circular dependency in Typescript
body: <p>The code below has a circular dependency issue. A -&gt; B -&gt; A</p>
  <h1>Reproducible problem:</h1>
  <p>/a.ts</p>
  <pre><code>import {B} from './b';
  import { Service } from './test';
  @Service
  export class A {
      static id = 'A'
  
      constructor(b: B) {
      }
  }
  </code></pre>
  <p>/b.ts</p>
  <pre><code>import {A} from './a';
  import { Service } from './test';
  @Service
  export class B {
      static id = 'B'
      constructor(a: A) {
      }
  }
  </code></pre>
  <p>/test.ts</p>
  <pre><code>export function Service(target: any) {
  // purposefully empty 
  }
  </code></pre>
  <p>/index.ts</p>
  <pre><code>import 'reflect-metadata'
  import {A} from './a'
  import {B} from './b'
  
  const paramTypesA = Reflect.getMetadata('design:paramtypes', A);
  const paramTypesB = Reflect.getMetadata('design:paramtypes', B);
  
  
  console.log('paramTypesA is', paramTypesA)
  console.log('paramTypesB is', paramTypesB)
  </code></pre>
  <h1>Expected output</h1>
  <pre><code>paramTypesA is [ [Function: B] { id: 'B' } ]
  paramTypesA is [ [Function: A] { id: 'A' } ]
  </code></pre>
  <h1>Actual output</h1>
  <pre><code>paramTypesA is [ [Function: B] { id: 'B' } ]
  paramTypesB is [ undefined ]
  </code></pre>
  <h1>Question: How to achieve the expected output</h1>
  <p>The goal is to detect circular dependency. But we also want to know what parameter exactly causes the circular dependency.</p>
  <p>If reflect-metadata only output undefined, it is impossible to know which parameter provokes the circular dependency.</p>
  <p>I already checked <a href="https://stackoverflow.com/questions/48080152/detecting-circular-dependencies-in-typescript">Detecting circular dependencies in TypeScript</a> but this didn't help</p>