Finding Unused Exports with ts-prune
There’s a tool called ts-prune
that helps identify and remove these unused exports for Typescript code-bases!
What is ts-prune?
ts-prune
is a CLI tool that reads your TypeScript configuration file and lists all the unused exports in your source files. I use it in all my personal projects, as it helps me look for unused files which helps me find what I need quicker.
Installing ts-prune
You can install ts-prune
via npm or yarn as a development dependency:
# npm
npm install ts-prune --save-dev
# yarn
yarn add -D ts-prune
Usage
You can integrate ts-prune
into your project by adding a script to your package.json
file:
{
"scripts": {
"find-deadcode": "ts-prune"
}
}
Then, you can run it with:
npm run find-deadcode
If you want to specify a different TypeScript configuration file:
ts-prune -p tsconfig.dev.json
Why ts-prune is Useful
- Automatic Detection: It detects unused exports without having to manually scan your files.
- Easy Integration: It’s straightforward to integrate into your existing build process.
- Improved Code Quality: By eliminating unused exports, you reduce clutter and potential maintenance headaches.
Limitations with Next.js
While ts-prune
is great for general TypeScript projects, it struggles with export default Page
in Next.js. Because Next.js uses export default
for special files like page.tsx
or layout.tsx
, ts-prune
mistakenly marks these as unused even when they are indeed being used by the framework.
Workaround
Currently, there isn’t a perfect solution to this issue other than manually ignoring these files or configuring your project to exclude Next.js-specific files from the check.
Conclusion
ts-prune
is a valuable tool for keeping your TypeScript projects clean and efficient. However, if you’re working with Next.js, you’ll need to be mindful of its limitations.
For more information, check out the ts-prune documentation.
My Technical Skills

AWS

JavaScript

TypeScript

React

Next.js

Cypress

Figma
