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.
Use this in combination with one of the external tools below to cover exports as well.
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.
- Quick audit, no setup:
ts-prune
orfind-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
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.
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.
- Ensure the project compiles with
tsc -p tsconfig.json
(tools rely on it). - Install a tool above as a dev dependency:
npm i -D ts-prune
- Add an NPM script:
"deadcode": "ts-prune | tee /dev/tty | test $(wc -l) -eq 0"
- Run
npm run deadcode
locally and in CI. - Inspect the list, delete or un-export the reported items.
- (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