CLI tool to auto-fix verbatimModuleSyntax
errors in your TypeScript project:
error TS1484: 'SomeType' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
verbatimModuleSyntax
is a TypeScript config introduced in v5. Enabling it is highly recommended as it enforces explicit type imports/exports, ensuring predictable compilation.
Previously, TypeScript automatically detected and removed type-only imports/exports during compilation. But this could lead to runtime issues if the imported modules had side effects. To address this, verbatimModuleSyntax
was introduced to require explicitly marked type imports/exports, making the code more maintainable and resilient against unexpected behavior during compilation.
However, enabling verbatimModuleSyntax
may initially produce many errors related to missing type annotations. Fixing these manually can be tedious so this command-line tool automates the process for you.
Ensure TypeScript v5 is installed in your project, then run the following command in the project directory, specifying the path to your tsconfig.json
.
[!WARNING] This command will modify your files. Be sure to back up or commit your changes beforehand.
npx fix-verbatim-module-syntax ./tsconfig.json
After running the command, add "verbatimModuleSyntax": true
to your tsconfig.json
.
To preview changes without modifying files, use the --dry
flag:
npx fix-verbatim-module-syntax --dry ./tsconfig.json