When working with TypeScript, you might have noticed something odd:
You delete a
.tsfile β but its compiled.jsand.d.tsfiles are still hanging around in yourdist/folder... π€¨
If you're like me, your first instinct is:
βWait... shouldn't
tsctake care of this automatically?β
Unfortunately, no.
π€ Why This Happens
The TypeScript compiler (tsc) compiles .ts files to .js, .d.ts, and .map.js, but it does not clean up old files. If you delete a source file, the compiled version stays in your output directory.
That leftover file might:
- Still get imported
- Cause your app to crash unexpectedly
- Create hard-to-track bugs
- Waste space, especially in limited environments
π οΈ The Fix: tsc-clear
To fix this, I built tsc-clear:
A lightweight Rust-powered CLI that automatically cleans up your dist/ folder by removing any compiled files (.js, .d.ts, .map.js) that no longer have a corresponding .ts file in your project.
β¨ Key Features
- π§Ή Auto-cleans stale files after compilation
- β‘ Fast β written in Rust for high performance
- βοΈ No configuration needed β reads
tsconfig.jsonto detect youroutDir - πͺ Windows only for now (cross-platform support planned)
π¦ Install It Globally
npm install -g tsc-clear
π Example Usage
Update your package.json scripts like this:
{
"scripts": {
"start": "tsc && tsc-clear src && node dist/index.js"
}
}
tsc-clear runs after tsc and before node, ensuring that your output directory is clean and safe.
π§ Why I Built It
I was working on a project where I kept removing and renaming files β and everything looked fine... until I ran the app and hit an error caused by a file I had deleted days ago π΅
The old .js file was still sitting in dist/, silently breaking everything.
After searching for a solution and finding nothing lightweight, native, and easy to use β I decided to build tsc-clear for myself.
Now Iβm sharing it with you. β¨
β TL;DR
- TypeScript doesn't clean up
dist/ - Old files can break your app or cause unexpected behavior
-
tsc-clearsolves this in one command
π Links
- GitHub Package Repo: github.com/SignorMassimo/tsc-clear
- GitHub Rust Repo: github.com/SignorMassimo/tsc-clear-rust
- npm: npmjs.com/tsc-clear
Thanks for reading!
Let me know if this tool helped you β or drop a star β on GitHub if you like it!
Happy coding! π§Όπ»
Top comments (1)
Deleted files lingering in dist/ usually means one of three things:
CDN cache invalidation lags β CloudFront does not purge instantly. If your CI/CD deletes from origin S3 and immediately expects the CDN edge to reflect that, you'll see ghost files for the cache TTL window. Versioned asset names (immutable) sidestep this entirely.
Mount layer caching β when an S3 bucket is mounted as a drive letter or via FUSE, the local filesystem cache can hold open file handles even after the underlying object is deleted. Explorer shows a phantom file until the cache flushes. Closing the mount + reopening usually reveals the truth.
Versioning silently holding old objects β once versioning is on (which is the right call for safety), deletes only create a delete-marker, not an actual remove. The old versions live on until lifecycle expires them. Drive-letter mount with versioning visible lets ops see and recover old versions without writing custom code.
Mount layer for ops seeing actual bucket state (not cached): ScsDriver