Skip to content
Fallow home
All docs pages

Docs home

Dead code

Find unused files, exports, types, class members and enum members with fallow dead-code, and remove them in a safe order.

Code that nothing uses still costs you, because people read it, tests run it, and a search shows it. In the demo project, nothing calls cancelOrder in src/services/orders.ts. It looks like part of the order API, so the next person who changes Order must keep it working too. fallow dead-code finds the files and the exports that your project does not use, so that you can delete them.

npx fallow dead-code

What fallow finds

The demo project has no type alias, enum or class. To show these findings too, this page adds one file, src/services/order-book.ts, and imports Channel and OrderBook in src/index.ts:

// src/services/order-book.ts
import type { Order } from "./orders";

export type OrderStatus = "open" | "paid" | "cancelled";

export enum Channel {
  Web = "web",
  Store = "store",
  Phone = "phone",
}

export class OrderBook {
  private orders: Order[] = [];

  add(order: Order): void {
    this.orders.push(order);
  }

  clear(): void {
    this.orders = [];
  }
}

src/index.ts reads Channel.Web and Channel.Store, and calls add. The report:

  3 entry points detected (2 package.json, 1 plugin)

── Unused Code ─────────────────────────────────────

● Unused files (1)
  src/utils/legacy-currency.ts
  Files not reachable from any entry point
  https://fallow.tools/docs/explanations/dead-code/#unused-files

● Unused exports (3)
  src/lib/format.ts
    :3 formatPercent
  src/lib/index.ts
    :1 formatPercent (re-export)
  src/services/orders.ts
    :18 cancelOrder
  Exported symbols with no known consumers
  https://fallow.tools/docs/explanations/dead-code/#unused-exports
  To auto-fix: fallow fix --dry-run
  To suppress: // fallow-ignore-next-line unused-export

● Unused type exports (1)
  src/services/order-book.ts
    :3 OrderStatus

● Unused enum members (1)
  src/services/order-book.ts
    :8 Channel.Phone

● Unused class members (1)
  src/services/order-book.ts
    :18 OrderBook.clear

✗ 1 file · 3 exports · 1 type · 1 enum member · 1 class member · 1 unused dependency · 1 unlisted dependency (0.09s)
PartMeaning
3 entry points detectedThe entry points where fallow starts. When fallow finds none, this line starts with ⚠ No entry points detected, and the findings are not reliable.
● Unused files (1)The issue type and the number of findings of that type.
:3 formatPercentThe line and the name of one finding.
(re-export)No file imports the name through this barrel file. Remove this line. When the original export is also in the list, no file uses it: remove it too. When it is not in the list, a direct import uses it: keep it.
The URLThe page that explains the issue type.
To auto-fix, To suppressThe command that fixes the findings, and the comment that hides one finding.
✗At least one finding has the severity error, so the command exits with code 1.

The last line also counts two dependency findings, left-pad and uuid. Dependencies explains them. fallow dead-code has more issue types, for example unresolved imports, duplicate exports and framework findings. Issue types lists each one.

How fallow decides

Each finding above is a fact about the module graph: fallow starts at the entry points and follows each import, also through barrel files. Entry points and reachability explains that walk. This section covers the cases that the walk alone does not settle. Fallow decides each case from the source text. The opt-in type-aware analysis adds the TypeScript type checker for the cases that syntax cannot decide.

Types and enum members

A type-only import, such as import type { Order } from "./orders", counts as a use of the export, the same as a value import. So OrderStatus is unused only because no file names it. For an enum that is in use, fallow checks each member. Channel.Phone is an unused enum member, because no code reads it. Object.values(Channel) and other uses of the whole enum count for each member.

Types in the signature of an export

A type that only the signature of another export in the same file uses is also an unused type when no other file imports it:

export type ButtonProps = { label: string };

export const Button = (props: ButtonProps) => props.label;

You can remove the export from ButtonProps. TypeScript accepts the file, and the declaration output of the file includes the local type.

Fallow does not report such a type in two cases:

  • The project of the file emits declarations. One config in the nearest folder with a tsconfig*.json sets declaration or composite and does not set noEmit. Or the nearest package.json names a .d.ts file in types, typings, typesVersions or a types export condition. Without the export, the declaration of another module that holds the value with an inferred type can fail with TS4023.
  • The private-type-leak rule is on for the file. Without the export, that rule reports the signature.

A config that does not parse, or whose extends chain does not resolve, counts as a config that emits declarations. A build tool that emits declarations without a tsconfig flag or a types field (for example tsdown with dts: true and exports that name only .js files) is not detected. In that project, keep the export when another module holds the value with an inferred type. fallow dead-code --trace <file>:<Type> names the exports whose signatures use the type and the reason, and the JSON trace has the object kept_by_signature. To not report a type that its own file uses, set ignoreExportsUsedInFile to { "type": true, "interface": true }.

Class members

OrderBook.clear is an unused class member: no code calls it. A use through this inside the class counts, so a helper method that only add calls through this is not reported. Fallow checks public methods, public properties and static methods. It does not report these members:

  • private members and #private members.
  • protected members. The opt-in rule unused-protected-class-member checks them.
  • Members with a decorator, such as @Get() or @Input(). A decorator tells fallow that a framework calls the member. To check the members of a decorator that is only a label, list it in ignoreDecorators.
  • Lifecycle methods of React, Angular and custom elements, such as componentDidMount and ngOnInit, and the methods that an active plugin knows, such as ionViewWillEnter of Ionic. React also reads the static properties defaultProps, displayName, propTypes, contextTypes and childContextTypes of a class component, so fallow does not report them. Add other names that a framework calls to usedClassMembers.

A call through a parent class counts for each override in a child class. When code uses the whole object, for example Object.keys(book) or { ...book }, fallow counts each member as used.

Exports of an entry point

Fallow does not report the exports of an entry point, because code outside your project can use them. When a plugin knows the names that a framework reads, fallow checks the other exports of that file. Entry points and reachability explains both cases.

Imports of a whole module

For import * as m from "./m", fallow counts only the names that the code reads, such as m.a. Code that reads each key, such as Object.keys(m), uses each export. Entry points and reachability has the table of import forms.

Findings in unused files

An unused file can also hold unused exports, types and members. Fallow does not list these findings, because the delete of the file removes them. To see this, add a second old file, src/utils/legacy-report.ts, that imports toGuilders from src/utils/legacy-currency.ts. Both files are unused, and the report ends with a count:

✗ 2 files · 3 exports (0.06s)
  2 findings in unused files are hidden · --show-cascade shows them

To list them, add --show-cascade:

npx fallow dead-code --show-cascade --unused-files --unused-exports
● Unused exports (5)
  src/utils/legacy-currency.ts (2)
    :1 toGuilders
    :3 fromGuilders
  src/lib/format.ts
    :3 formatPercent

The showCascade key in the config file does the same for each run. The health metrics count these findings with and without the option.

Act on a finding

A finding is a strong hint, but you delete the code, so check each finding first. --trace shows why fallow reports an export:

npx fallow dead-code --trace src/services/orders.ts:cancelOrder
  UNUSED cancelOrder in src/services/orders.ts

  File: reachable
  Namespace: value
  Reason: No references found, export is unused

Namespace: value tells you that cancelOrder is a value. For OrderStatus, the trace says Namespace: type. For a file, use --trace-file. It shows the exports of the file, its imports and the files that import it, also for an unreachable file:

npx fallow dead-code --trace-file src/utils/legacy-currency.ts
  UNREACHABLE src/utils/legacy-currency.ts

  Exports (2):
    export toGuilders [unused]
    export fromGuilders [unused]

Find why fallow reports a finding shows each trace command. When the trace confirms the finding, pick the action that fits:

SituationAction
Nothing uses the code.Delete the file, the export or the member.
Only the file itself uses an export.Remove the export keyword and keep the code.
A tool or a framework loads the code, and fallow does not know it.Correct the entry points or the plugin. See Fix a false positive.
You keep the code on purpose.Add a suppression comment. See Suppress a finding.

Code that is unused and also duplicated is a good start: --include-dupes lists it. fallow fix removes unused exports and unused enum members for you. Run fallow fix --dry-run first. Remove unused code with fallow fix explains what it changes.

Work in this order

  1. Delete the unused files.
  2. Run fallow dead-code again.
  3. Remove the unused exports, types and members.

The order matters, because an import in an unused file still counts as a use of an export. When you delete the file, the exports that only that file imported become unused, and the second run shows them.

Fail a run

Each issue type on this page has the severity error by default, so one finding makes fallow dead-code exit with code 1. The demo run above exits with code 1. A finding at the severity warn shows in the report and keeps the exit code at 0, unless you add --fail-on-issues:

npx fallow dead-code --fail-on-issues

To report an issue type without a failure, set its rule to warn in the config file. For example, a team that cleans the class members later:

// .fallowrc.jsonc
{
  "rules": {
    "unused-class-members": "warn"
  }
}

To fail only on the dead code that a change adds, see Changed code and baselines.

What fallow cannot see

  • A path that exists only when the code runs, for example import(name) with a variable. Name these files in dynamicallyLoaded.
  • A use through reflection, for example a dependency injection container that reads class names.
  • A use outside the project, for example a file that only a deploy script outside the repository runs.
  • A file that only tests import is reachable in a normal run. Use production mode to check the code that ships.

For each known case and its workaround, see Known limits of fallow.

Reference