Finding and cleaning unused code in your Typescript project

https://asciinema.org/a/liQKNmkGkedCnyHuJzzgu7uDI

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

--

--

--

Hi, I’m Nadeesha. I work with computers a lot. I write about software development, and a few other things.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nadeesha Cabral

Nadeesha Cabral

Hi, I’m Nadeesha. I work with computers a lot. I write about software development, and a few other things.

More from Medium

What TypeScript Really Means for Our JavaScript Code

TypeScript vs JavaScript: What’s the Difference?

Typescript vs. Javascript: The Key Differences You Should Know in 2022

TypeScript vs. JavaScript: The Key Differences You Should Know — Flatlogic Blog

Configuring Typescript: Basics