Automatically Remove All Unused Imports in a TypeScript Project

A quick workaround solution for Typescript imports

Denis Cangemi
Bumpware

--

The Problem

While programming with Typescript is very usual that at some point in your project development, you’ll need to remove all unused imports in order to make the code cleaner and more readable.

Unfortunately, I’m a Visual Studio fan and there’s no way to automatic remove the imports, at the time this article was written, and the only way is to open each individual *.ts file and hitting CTRL + Shift + O on Windows/Linux.

Let’s see a workaround solution.

The Solution

Using the tslint command line tool, in conjuction with the tslint-etc rules, it’s possible to automatically detect and remove all unused imports in the directory, recursively.

If you have a large project, the process can take some time to run and I suggest to double-check all files for correctness once the fix process is complete.

Install Required Tools

Install the following node packages required for this process

$ npm install -g typescript tslint tslint-etc

Create a tslint config file

--

--

Denis Cangemi
Bumpware

IT Project Manager | Writing about Coding and Project Management | ISIPM-Base® / PSM™ I / SFC™