Is there a way to automatically fix `import type` errors on TypeScript when using "importsNotUsedAsValues": "error"?

21.8k views Asked by At

I have a big TypeScript project that I started without using the "importsNotUsedAsValues": "error" compiler flag. As the project grew, I decided it is a good idea in terms of optimization to enforce import type when an import is not being used as a value.

Enabling this works fine and even makes my IDE auto import types using import types when possible, and it can also automatically fix open files that do not import types as types only, but not the entire project in one go, and I have so many of those cases in the project that fixing them manually is inconceivable.

I've been reading ESLint, TSC and similar tools documentations trying to find a way I could use them to automatically fix the imports, but with no success. Does anyone know a tool that could automatically fix those issues?

1

There are 1 answers

0
Bruno Finger On

After a little more searching, I found ESLint rule consistent-type-imports which basically ensure the same as "importsNotUsedAsValues": "error" TypeScript compiler flag but on ESLint level.

Enabling it was as simple as adding

'@typescript-eslint/consistent-type-imports': 'error',

to rules section in my ESLint configuration file.

To autofix, now I then simply needed to run

eslint --fix .