Class-level

Overview

Depth of Inheritance Tree (DIT) measures how far down a class sits in the inheritance hierarchy. A class with no extends clause has DIT = 0. A class that directly extends another class has DIT = 1. Each additional level of inheritance adds 1.

Deeper inheritance chains mean a class potentially inherits more behaviour from ancestors, making it harder to predict the class's full behaviour and increasing the chance that changes to ancestors break descendants.

Formula

DIT(C) = 0    if C has no parent
DIT(C) = 1 + DIT(parent)    otherwise

How tsmetrics computes DIT

tsmetrics resolves the full chain within the analyzed files using memoized recursive lookup. Cycle detection prevents infinite loops in the rare case of circular declarations.

When a parent class is defined outside the analyzed files (e.g. a third-party library or a built-in like Error), tsmetrics treats that parent as the root of the hierarchy. The child class therefore has DIT = 1, not 0, reflecting that it does inherit from something even if the depth cannot be fully resolved.

Interpretation

DITInterpretation
0No inheritance — standalone class
1 – 2Shallow hierarchy — generally well-structured
3 – 4Moderate depth — review for excessive coupling
5+Deep hierarchy — consider flattening or using composition

Thresholds

DIT is an informational metric with no default warning or error thresholds. It is not configurable via tsmetrics.yaml.

Example

TypeScript
class Animal {}                       // DIT = 0
class Mammal extends Animal {}         // DIT = 1
class Dog extends Mammal {}            // DIT = 2
class Labrador extends Dog {}          // DIT = 3

// External parent — treated as root
class AppError extends Error {}         // DIT = 1

Usage in detection strategies

References

Edit on GitHub