Make Prettier organize your imports using the TypeScript language service API.
Make sure that your import statements stay consistent no matter who writes them and what their preferences are.
A plugin that makes Prettier organize your imports (i. e. sort and remove unused imports) using the
organizeImportsfeature of the TypeScript language service API. This is the same as using the "Organize Imports" action in VS Code.
npm install --save-dev prettier-plugin-organize-imports
typescriptare peer dependencies, so make sure you have those installed in your project.
The plugin will be loaded by Prettier automatically. No configuration needed.
Files containing the substring
// tslint:disable:ordered-importsare skipped.
This plugin acts outside of Prettier's scope because "Prettier only prints code. It does not transform it.", and technically sorting is a code transformation because it changes the AST (this plugin even removes code, i. e. unused imports). In my opinion however, the import statements are not really part of the code, they are merely directives that instruct the module system where to find the code (only true as long as your imports are side-effects free regarding the global scope, i. e. import order doesn't matter), comparable with
usingdirectives in C# or
#includepreprocessing directives in C. Therefore the practical benefits outweigh sticking with the philosophy in this case.