Angular has long supported a showCircularDependencies
build flag that identifies circular dependencies within classes. But do these warnings pose a real issue? Let's consider an example.
import {MyOtherService} from './my-other-service.ts';
@Injectable()
class MyService {
constructor(private otherService: MyOtherService)
}
function calculate(): void {
// some method that should be exported so it can be used without the service instance
}
export {MyService, calculate}
and another file:
import {calculate} from './my-service.ts'
@Injectable()
class MyOtherService {
constructor() {
calculate();
}
}
This will result in a warning:
my-service.ts -> my-other-service.ts -> my-service.ts
Question is, is this truly a problem? JavaScript seems to handle imports efficiently and avoid memory leaks in this scenario.
Additionally, in cases where circular dependencies could cause issues (e.g., MyOtherService
injecting MyService
), Angular will throw hard build errors, preventing compilation altogether.
(It's worth noting that the option to display these warnings will be deprecated starting with Angular 14 as indicated in the provided documentation link.)