Is circular type reference a bad thing to do In TypeScript?

1.8k Views Asked by At

I encountered below code snippets which contain a cyclic importing dependency on type reference.

// Foo.ts
import { Bar } from './Bar';
export interface Foo {
  isBarOK: (bar: Bar) => boolean;
}

// Bar.ts
import { Foo } form './Foo';
export class Bar {
  protected readonly foo: Foo;
}

This is a simplified version but it explains the case I encountered. tsc has no problem in compiling the code, but I got a warning from the import/no-cycle ESLint rule. We can see that Foo and Bar references each other for typing. Is this a bad practice? What impacts does it imply?

1

There are 1 best solutions below

1
arslan2012 On BEST ANSWER

pure typescript file will not be outputted into your bundle, so you can safely ignore it.

but be careful with javascript file, circular import might mess up tree shaking