Concepts

The ideas behind tdepend, and what its numbers mean

Rules as Fitness Functions

A fitness function is an automated check that keeps an architecture aligned with its intended design. In tdepend the fitness functions are rules: statements about which imports are allowed.

{
  "type": "forbidden",
  "id": "LAYER-CONTROLLER-REPO-001",
  "from": ["src/**/*.controller.ts"],
  "to": ["src/**/*.repository.ts"],
  "message": "Controllers call services, not repositories."
}

tdepend check evaluates every rule and exits 1 when an import breaks one, so the rule holds on every pull request. There are four rule types: forbidden, layers, private and no-cycles.

A rule points at one import to fix. That is why rules can fail a build and metrics cannot: a number such as distance tells you where to look, not what to change.

The Import Graph

Everything rests on one graph: which file imports which. tdepend builds it with TypeScript's own module resolver, using each file's nearest tsconfig. Path aliases, package exports, project references and workspace packages resolve exactly as they do for the compiler.

A workspace package is followed to its source files, whether or not it has been built. Every import is also given a class, such as internal, external or unresolved, so nothing disappears silently.

Runtime and Type-Only Imports

Not every import runs. The compiler removes imports that are only used as types, so they can't cause a runtime cycle.

import type { User } from './user';        // erased: never runs
import { UserService } from './user.service';  // loads, if used as a value

tdepend decides this for each import, and it is conservative: an import counts as loading its target unless tsc, esbuild and swc would all drop it. So an import reported as erased is erased by every one of them. tdepend explain <file> shows the verdict and the reason for each import in a file.

Components and Packages

Architecture is about groups of files, not single files. A component is such a group: a folder matched by a pattern like src/*, or a NestJS module folder. A package is a workspace package.

tdepend aggregates the file graph to both levels. Rules, metrics and cycles can then be read per file, per component or per package. Files that belong to no component are collected in a (root) group.

Coupling and Instability

  • • Afferent coupling (Ca): how many others depend on this one
  • • Efferent coupling (Ce): how many others this one depends on

Instability = Ce / (Ca + Ce)

An instability near 0 means many things depend on the component and it depends on little, so it is costly to change. Near 1 means the opposite: it is free to change. Neighbours are counted at the level being measured, and npm packages are not counted.

Abstractness

Abstractness = abstract declarations / counted declarations

In Java an interface is an abstraction. In TypeScript most interfaces only describe data. So by default tdepend counts an interface or abstract class as abstract only when something implements or extends it, and it leaves fields-only declarations such as DTOs out of the ratio altogether.

Set metrics.abstractness to "jdepend" for the classic definition, where every interface, type alias and abstract class is abstract.

Distance

Distance = | Abstractness + Instability − 1 |

The main sequence is the line where the two balance: a component is either stable and abstract, or unstable and concrete. Distance is how far a component sits from that line.

  • • Zone of pain: stable and concrete (A and I near 0). Many depend on it, and it is hard to extend.
  • • Zone of uselessness: abstract and unstable (A and I near 1). Abstractions nobody depends on.

Distance is a signal, not a verdict. metrics.thresholds.distance lists the components beyond a value in the analyze report. It never changes the exit code.

Cycles

A cycle is a set of files, components or packages that all reach each other through imports. tdepend finds them with Tarjan's strongly connected components algorithm, which runs in O(V + E) time.

Knowing that twenty files form a cycle doesn't tell you what to fix. So each cycle is reported with its shortest loop and the exact import behind every step:

Component cycles (1):
  src/orders → src/users → src/orders
    src/orders/orders.controller.ts:2:33  '@users/repositories/users.repository' → src/users/repositories/users.repository.ts
    src/users/users.service.ts:3:31  '../orders/orders.service' → src/orders/orders.service.ts

By default only imports that run are considered, so a loop made of type-only imports is not a cycle. Cycles make components impossible to test or change in isolation, and can cause initialisation-order bugs.

Baselines

A new rule usually finds violations that already exist. A baseline records them, with a count, so the rule can be enforced from today: existing violations are accepted, and only new ones fail the build.

A violation is matched by rule, file, target and specifier, not by line number, so editing a file doesn't disturb it. tdepend baseline --prune shrinks the baseline as violations are fixed, and never accepts a new one.