madge: Madge for typescript: skipping type imports still produces wrong result while searching for circular dependencies

I have moduleA.ts

import * as Life from './moduleB';

export interface Person {
    age: number;
    health: number;
    // ...
}

export const live = (p: Person) => Life.goToDoctor(Life.getOld(p))

console.log(live({
    age: 65,
    health: 0.6,
}))

and moduleB.ts:

import { Person } from './moduleA'

export const getOld = (p: Person): Person => ({
    ...p,
    age: p.age + 1
})

export const goToDoctor = (p: Person): Person => ({
    ...p,
    health: Math.min(p.health * 1.2, 1)
})

Following advice from @pahen (see (https://github.com/pahen/madge/issues/231)) I enabled skipping type imports in typescript (since cyclic deps are allowed in this case), so I added to my .madgerc file in root directory:

{
	"detectiveOptions": {
		"ts": {
			"skipTypeImports": true
		}
	}
}

but I’m still getting error:

Processed 2 files (584ms) 

✖ Found 1 circular dependency!

1) moduleA.ts > moduleB.ts

Is there something I am missing, or is it a bug?

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 17
  • Comments: 22 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Same for import type syntax:

foo.ts

import type { Bar } from 'bar'

export type Foo = {
  bar: string;
};

export function doFoo() {
  return true;
}

bar.ts

import type { Foo } from 'foo'

export type Bar = {
  bar: string;
};

export function doBar() {
  return true;
}

madge config:

{
    "detectiveOptions": {
      "ts": {
        "skipTypeImports": true
      }
    },
    "baseDir": "./src",
    "fileExtensions": [
      "ts"
    ],
    "excludeRegExp": [
      "^.*__tests__.*$",
      "^.*__mocks__.*$"
    ],
    "tsConfig": "tsconfig.json"
  }

Output:


✖ Found 1 circular dependency!

1) bar.ts > foo.ts

Back again… this time I needed to add a “tsx” block to the .madgerc:

{
  "detectiveOptions": {
    "ts": {
      "skipTypeImports": true
    },
    "tsx": {
      "skipTypeImports": true
    }
  }
}

Not sure if it’s better to comment here or on #340, but I’ve found that the specific type syntax matters:

import { type TheType } from './types'; // is INCLUDED when `skipTypeImports: true`
import type { TheType } from './types';  // is EXCLUDED when `skipTypeImports: true`

I have also tried both syntaxes with the config provided above (https://github.com/pahen/madge/issues/232#issuecomment-1602761190). Only when the using import type { ...} did the following minimum options work for my TS/TSX project:

{
  "detectiveOptions": {
    "ts": {
      "skipTypeImports": true
    },
    "tsx": {
      "skipTypeImports": true
    }
  }
}

For reference, I’m invoking madge using only a small portion of my project, and with command similar to:

pnpm dlx madge --include-npm src/pages/Page1/index.tsx --debug

i can also confirm something wrong ! "madge": "^3.12.0", using skipTypeImports not work image

Actually the test look like thats index.jsx

import { Test } from './1.tsx';

1.tsx

import { Dictionary } from './2.tsx';
export class Test implements Dictionary {
}

2.tsx

export interface Dictionary {}

2.tsx should not considerate because import only type.

It seems like a dup of #330, could you confirm please @kamiazya ?

work if we add type : ex import type { } from ".../";

I am still encountering this issue with madge v5.0.1. Madge behaves as if the configuration skipTypeImports: true doesn’t exist with typescript v4.4.2. Any advice would be greatly appreciated.