Skip to content

Instantly share code, notes, and snippets.

@eonist
Created August 3, 2025 22:42
Show Gist options
  • Save eonist/00ad0af1b89f1391039bfea3f9f4bd46 to your computer and use it in GitHub Desktop.
Save eonist/00ad0af1b89f1391039bfea3f9f4bd46 to your computer and use it in GitHub Desktop.
Find dead export code in typescript

Ways to locate unused exports in a TypeScript codebase

1. Built-in TypeScript compiler flag

The TypeScript compiler can already warn about unused locals and unused parameters with the flag below, but it does not report exports that are never imported elsewhere.

// tsconfig.json
{
  "compilerOptions": {
    "noUnusedLocals": true,
    "noUnusedParameters": true
  }
}

Use this in combination with one of the external tools below to cover exports as well.

2. Purpose-built CLI tools

Tool How it works Typical command Highlights
ts-prune[1] Parses your tsconfig.json, walks the AST and lists exports never imported from any other module. npx ts-prune Zero-config, very fast, can be piped to grep to ignore “used in module” lines.
ts-unused-exports[2] Uses the TypeScript compiler API to find unused symbols; offers many tuning flags. npx ts-unused-exports tsconfig.json Options to ignore test files, exit with unused count, show line numbers, etc.
tsr (TypeScript Remove)[3] Performs tree-shaking–style analysis, then optionally edits files to drop the dead exports/imports. npx tsr --remove Can delete code automatically; good performance on large repos.
Knip[4] Scans for unused dependencies, files and exports; supports monorepos. npx knip --fix flag can delete exports; also finds unused packages.
find-unused-exports[5] Simple glob-based scanner that respects .gitignore. npx find-unused-exports Lightweight; works in CI to prevent dead code regressions.

All of them read your tsconfig.json, so path aliases and module resolution match your build.

Choosing a tool

  • Quick audit, no setup: ts-prune or find-unused-exports
  • Need many options / ignore patterns: ts-unused-exports
  • Want auto-removal: tsr (safe in version-controlled projects)
  • Monorepo, also check unused deps: knip

3. Editor plug-ins

If you prefer interactive feedback inside VS Code:

  • Find Unused Exports extension shows a panel listing dead exports and lets you jump to them[6].

This is handy for ad-hoc cleanup while coding.

4. Lint-time integration

You can wire these tools into CI so new pull requests cannot add dead exports:

// package.json
{
  "scripts": {
    "lint:exports": "ts-prune --error"
  }
}

Run npm run lint:exports in your CI pipeline and fail the build if output is non-empty.

5. Typical workflow

  1. Ensure the project compiles with tsc -p tsconfig.json (tools rely on it).
  2. Install a tool above as a dev dependency:
    npm i -D ts-prune
  3. Add an NPM script:
    "deadcode": "ts-prune | tee /dev/tty | test $(wc -l) -eq 0"
  4. Run npm run deadcode locally and in CI.
  5. Inspect the list, delete or un-export the reported items.
  6. (Optional) For bulk cleanup, run npx tsr --remove and review the diff.

Following this process keeps the repository tidy, speeds up type-checking, and avoids shipping bloated bundles.

[1] https://github.com/nadeesha/ts-prune [2] https://www.npmjs.com/package/ts-unused-exports [3] https://github.com/line/tsr [4] https://knip.dev/typescript/unused-exports [5] https://www.npmjs.com/find-unused-exports [6] https://marketplace.visualstudio.com/items?itemName=iulian-radu-at.find-unused-exports [7] https://effectivetypescript.com/2020/10/20/tsprune/ [8] https://kazushikonosu.io/en/typescript-remove-tsr [9] https://www.reddit.com/r/webdev/comments/1groxau/tsremoveunused/ [10] https://dev.to/detoner777/finding-dead-code-in-the-typescript-project-3dm4 [11] https://knip.dev [12] https://news.ycombinator.com/item?id=41554014 [13] https://stackoverflow.com/questions/68546480/detect-unused-exported-symbols-in-a-a-typescript-eslint-webpack-based-build-envi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment