47

There is a new feature in TypeScript 2.8 that lets you "Organize imports": https://devblogs.microsoft.com/typescript/announcing-typescript-2-8-2/#organize-imports

Basically it does the following:

  • remove unnecessary import statements
  • sort the import statements

The page shows that the feature can be invoked in Visual Studio Code with the Shift+Alt+O keyboard shortcut. Does anybody know how this feature can be invoked in Visual Studio (2017)?

UPDATE:

From version 15.8 onward Visual Studio 2017 highlights unused imports by greying them out.

JSON Derulo
  • 9,780
  • 7
  • 39
  • 56
Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84

6 Answers6

56

Visual Studio Code has released a new feature in April last year which enable to organize imports on save. Can you try to update your current settings.json with the following changes:

"editor.formatOnSave": true,
"[typescript]": {
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    }
}

Hopefully this can be helpful and good luck!

JayKan
  • 3,966
  • 2
  • 20
  • 21
42

You can also use the following shortcut with your keyboard to "Organize imports":

Shift+Alt+O

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
Asif Billa
  • 821
  • 2
  • 15
  • 28
23

A slightly modified version of JayKan's answer but this worked for me in VSCode settings.json.

    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    }
Neil J
  • 239
  • 2
  • 4
  • I think this must be accepted answer. Time saver answer. – kodmanyagha Jul 19 '23 at 14:13
  • Yes this is working for me too. Although I later switched it off as it was annoying me by deleting unused imports. Generally helpful, but a disaster when you are moving code between files, and thereby losing the relevant imports in the "old" file. Also a bit miserable when you bring a set of imports in from a template file and then try to write code that uses them. The ones you haven't used yet, keep disappearing. – ProfDFrancis Jul 23 '23 at 09:19
19

This feature has now been added to Visual Studio 2017.

One can invoke it the following ways:

  1. Pressing Ctrl+R followed by Ctrl+G.
  2. Right-click in the code window and click Organize Imports in the context menu.
Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
2

In Visual Studio 2022 (might even be in previous releases too) the option Organize Imports in the editor's code context menu has been renamed to Remove and Sort Usings, which still does the same thing and also remains available using the shortcut CTRL+R followed by CTRL+G.

Just in case anyone overlooked it, as I did.

-1

This is a feature of Visual Studio Code and not Visual Studio 2017.

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
  • The Microsoft site says: "TypeScript’s language service now provides functionality to organize imports." So it should not be a feature of VS Code. Maybe it is not callable in VS 2017 yet. – Krisztián Balla Jul 19 '18 at 06:59
  • BTW. I use Resharper for Visual Studio. It has Code Cleanup function with import organizing feature for Typescript. However not so good as VS Code – Sergey Rusakov Jul 20 '18 at 08:04
  • VS Code merges and sorts multiple imports from same source into one line. Visual Studio doesn't. VS Code adds new imports as you type more accurately. VIsual Studio sometimes does, somethimes doesn't. Anycase, typescript in Visual Studio vs VS Code is a question of personal taste. – Sergey Rusakov Jul 21 '18 at 16:11
  • From version 15.8 onward Visual Studio 2017 now highlights unused imports by greying them out. – Krisztián Balla Aug 24 '18 at 07:41