Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use imports between workspaces

Error TS6202: Project references may not form a circular graph.

My structure: -packages --utils --api

utils/tsconfig.json

...
references: [../api/src]

api/tsconfig.json

...
references: [../utils/src]

I need use utils in api package and use some api in utils

like image 432
skripykez Avatar asked Jan 26 '26 21:01

skripykez


1 Answers

Circular dependencies are usually a hint, that your architecture is not yet sound.

Try one or multiple of the following:

  1. Create a third package, from which both depend and escalate the things needed by both into the higher layer.
  2. Add additional abstraction. If for example class "A" (from api) needs class "U" (from util) and class U also needs class A, create an interface for the more relevant parts of the more general class. From your name, util shold most likely not depend on API.

example:

// in api
class A {
  constructor(private u: U) {}
  fancyMethod() {}
}
// in util
class U {
  constructor(private a: A) {}
}

In this setting A from api depends on U from util and vice versa. To resolve this, create an Interface, that describes the required functionality from U's point of view and make it dependant on that:

// in api
class A implements FancyIf{
  constructor(private u: U) {}
  fancyMethod() {}
}
// in util
interface FancyIf {
  fancyMethod(): void;
}
class U {
  constructor(private a: Fancy) {}
}

This way, util does not need to depend on api, but it defined, what it needs to operate by providing an interface. The more specific class A from api then implements the interface to state, that it can fulfil the needs of the U class.

Hint: Also think about concepts like dependency injection.

like image 186
Michael K Avatar answered Jan 28 '26 16:01

Michael K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!