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.

Dependency cycle detected import/no-cycle

See original GitHub issue

Hello,

After an update from ‘2.22.1’ to ‘2.23.0’ I have this error

Dependency cycle detected import/no-cycle

I’m trying to import a TypeScript Type like this:

./Bar.ts

import useBar from './useBar';

export type Bar = {
  msg: string
}

./useBar.ts

import type { Bar } from './Bar';

Previously import type even if it was cycled import worked.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
raychariuscommented, May 17, 2021

An option would be great – I would love to have this rule included. And I have a feeling that this has broken eslint for a lot of people ❤️

As for import type creating runtime issues, it won’t, even with enums and classes, though using import with an enum will. I’ve created a small test repo that demonstrates this here.

In the documentation for import type, they say:

import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there’s no remnant of it at runtime. Similarly, export type only provides an export that can be used for type contexts, and is also erased from TypeScript’s output.

However, there is a very poorly worded section:

It’s important to note that classes have a value at runtime and a type at design-time, and the use is context-sensitive. When using import type to import a class, you can’t do things like extend from it.

It can lead to the impression that import type will have a value at runtime, but looking at the example below, it’s about the inability to extend the class as you would with an interface using import type.

If you’re good with a flag, I’m happy to take a look and send over a pull request

2reactions
raychariuscommented, May 16, 2021

Sorry, @ljharb, but this is wrong. The purpose of this rule is to avoid possible issues at runtime with circular dependencies. Using import type will never lead to that. Moreover, I disagree about this always being bad architecture when it comes to importing types. There are plenty of cases when this is more than appropriate.

At the very least, it should be configurable to exclude import type.

The makers of Madge know and understand that.

In any case, this has rendered this check useless for me because of this, which is actually really unfortunate. So, turning this rule off and integrating Madge into CI flow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

dependency cycle detected import/no-cycle
I am trying to set up API endpoints in ES6. In my main server file, I tried to import the router module but...
Read more >
eslint-plugin-import/no-cycle.md at main
Ensures that there is no resolvable path back to this module via its dependencies. This includes cycles of depth 1 (imported module imports...
Read more >
Dependency cycle detected in React component index file
So I read the docs for eslint's "import/no-cycle" rule: "Ensures that there is no resolvable path back to this module via its dependencies."...
Read more >
[Solved]-dependency cycle detected import/no-cycle-node.js
[Solved]-dependency cycle detected import/no-cycle-node.js ... This could be a direct reference (A -> B -> A) issue, which even you might be doing....
Read more >
Understanding import/no-cycle problem in eslint
The exact error thrown is: ESLint: Dependency cycle via ../types:7=>./Jobs:1(import/no-cycle) . File 1. jobs/Jobs. import { AuthJobs } from './ ...
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