Finding and cleaning unused code in your Typescript project

Typescript does a pretty good job of warning you against unused code in your modules, as long as you don’t export
it. Once you do, all bets are off and Typescript compiler assumes exporting something implies usage.
So, as a Typescript project grows, it’s fairly common for these unused exports to accumulate.
ts-prune
Enter ts-prune. It’s a small CLI that I built to analyze your tsconfig.json and warn you against unused exports.
npm install ts-prune -g
It’s built with the excellent ts-morph library and outputs all your unused exports into stdout.
Since ts-prune outputs to stdout, it’s pretty easy to use existing tools such as grep
, wc
and etc to manipulate the output as well. For example, to ignore the src/shared
path and get a count of unused exports, you would:
ts-prune | grep -v src/shared | wc -l
That’s it. Find the source at https://github.com/nadeesha/ts-prune