Skip to content

JS/TS: import type / export type counted as runtime imports — every Import Cycle we got was a false positive #3123

Closed

Description

@cbartens

Summary

The TS extractor emits an ordinary imports_from edge for type-only specifiers (import type / export type). TypeScript erases those at compile time and emits nothing at runtime, so any cycle that closes through them cannot exist.

On our repo (~7,900 nodes) this made 3 of 3 reported import cycles false. Since Import Cycles is a headline section of GRAPH_REPORT.md, that's a section we can't act on.

Related but distinct: #1193 is also a file-cycle false positive, but its cause is module names inside // comments. This one is about real import type statements being parsed correctly and then classified as runtime edges.

Minimal repro

Two files, nothing between them but type-only specifiers.

types.ts:

export type Variables = { requestId: string };
export type { RouterType } from "./v1.js";

v1.ts:

import type { Variables } from "./types.js";
export type RouterType = { v: Variables };
export function makeRouter() { return {} as RouterType; }
graphify extract . --code-only
graphify cluster-only . --no-label --no-viz

Actual

## Import Cycles
- 2-file cycle: `types.ts -> v1.ts -> types.ts`

Expected

No cycle. tsc emits no runtime import in either direction — both files' type-only specifiers are erased.

What it looked like on a real codebase

All three cycles reported for our API package:

2-file cycle: packages/api/src/types.ts -> v1.ts -> types.ts
3-file cycle: packages/api/src/inngest.ts -> middleware-stack.ts -> types.ts -> inngest.ts
3-file cycle: packages/api/src/middleware-stack.ts -> types.ts -> v1.ts -> middleware-stack.ts

Every edge closing those loops is type-only — types.ts lines 11–12 are export type { … } from "./v1.js" and "./inngest.js", and v1.ts:12, middleware-stack.ts:20, inngest.ts:9 are all import type { Variables } from "./types.js". The only runtime edges are v1 -> middleware-stack and inngest -> middleware-stack, and middleware-stack imports neither back.

The file even carries a comment saying the export type form was chosen deliberately for "zero runtime emit — no module graph cost". The tool reported the mitigation as the defect.

Suggested fix

Tag the edge rather than dropping it — the type dependency is real and useful for "what references this type", it just isn't a module-graph edge:

  • emit a distinct relation (e.g. imports_type), or keep imports_from with a type_only: true attribute;
  • exclude type-only edges from cycle detection.

Worth noting the same conflation likely inflates affected in the other direction: a type-only importer is reported as impacted by a runtime change.

Both cases to cover: import type { X } from "…" and export type { X } from "…", plus inline import { type X } / export { type X }.

Environment

  • graphify 0.9.50 (uv tool install "graphifyy[sql]")
  • Python 3.14.5, macOS arm64
  • TypeScript 5.x, pnpm workspace monorepo

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions