Configure, migrate, and verify projects

Fix a TypeScript Circular Import Without Hiding a Runtime Cycle

Diagnose a TypeScript circular import, separate type-only and runtime edges, and verify the change in emitted JavaScript.

Editorial illustration for Fix a TypeScript Circular Import Without Hiding a Runtime Cycle

A circular-import report is a reason to inspect dependencies, not proof that JavaScript will fail. Answer two questions in order: which source-file imports form the reported path, and which of those imports are needed as runtime values? An import type can remove a runtime edge that exists only to name a type. It cannot remove a value import that still forms a cycle.

Conceptual TypeScript source graphConceptual JavaScript graphuser.tspost.tsapp.tsuser.tspost.tsapp.tstype-onlytype-onlyruntime valueruntime valueno type-only arrows

Cover diagram: conceptual dependency graphs, not measured or generated compiler output. Dashed arrows represent type-only source imports; the solid arrow shows a separate value dependency that import type does not erase.

Trace the reported file path

Madge provides a diagnostic starting point for source-file loops:

npx madge --circular --extensions ts ./src

The Madge example reports a path through filenames. Open each import along that path, including indirect edges, and find where the imported name is used. Is it only an annotation or interface reference, or is it a function, class, or other value used by JavaScript? An import through a directory’s index.ts can make the path less obvious, so inspect that file too.

This procedure concerns imports between source files. A recursive type declaration within a file is a different question. So is a project-reference diagnostic; neither is resolved simply by classifying imports on a reported source-file path.

Keep type references without a runtime import

Suppose these two hypothetical files describe objects that refer to each other. import type requires TypeScript 3.8 or later:

// src/user.ts
import type { Post } from "./post";

export interface User {
  id: string;
  posts: Post[];
}
// src/post.ts
import type { User } from "./user";

export interface Post {
  id: string;
  author: User;
}

Each file names an interface from the other, but neither imports a JavaScript value from it. Type-only imports are erased, so these statements do not create runtime imports between the files. A source-file dependency report may nevertheless retain the loop: the TypeScript files still refer to one another. That report alone does not establish a module-loading cycle.

Do not replace a value import with import type merely to quiet a warning. If code calls an imported function or reads an imported constant, it needs a value. Two files that still import each other’s values still have a runtime cycle. Depending on the module system and execution order, initialization can then cause problems; it is not a guaranteed failure for every cycle. The runtime-cycle discussion makes that distinction explicit.

Move shared contracts when the file loop is unwanted

If the cross-imports exist only to share contracts, a separate module gives both implementation files a common place to import them:

// src/types.ts
export interface User {
  id: string;
  posts: Post[];
}

export interface Post {
  id: string;
  author: User;
}
// src/user.ts
import type { User } from "./types";

export function createUser(id: string): User {
  return { id, posts: [] };
}
// src/post.ts
import type { Post, User } from "./types";

export function createPost(id: string, author: User): Post {
  return { id, author };
}

In this example, user.ts and post.ts import contracts from types.ts instead of importing one another. The interfaces still describe a relationship between the objects; that relationship does not require a dependency between these implementation files. This is the narrower purpose of the shared-interfaces approach. If either implementation also imports the other’s runtime values, moving interfaces will not remove that edge. The value dependency needs its own design decision, such as moving code that combines the two modules into a third module.

Verify three different results

Run the repository’s existing type check and build. Then inspect the JavaScript that build actually produced for the affected files. The type check can establish that the intended interface references are accepted. If no JavaScript import corresponds to an edge intended to be type-only, that edge is absent from this build’s output. Neither observation proves that every runtime path works.

Rerun the cycle detector as well. If it still reports a source-file loop, inspect its remaining edges rather than assuming they are runtime imports. Where value imports remain, exercise the affected behavior under the project’s actual runtime and module system. A clean type check, an absent emitted import, and working runtime behavior are separate findings.

Find a note

Search by topic, title, or keyword.