Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke the "Organize imports" TypeScript feature in Visual Studio?

There is a new feature in TypeScript 2.8 that lets you "Organize imports": https://blogs.msdn.microsoft.com/typescript/2018/03/27/announcing-typescript-2-8/#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.

like image 864
Krisztián Balla Avatar asked Apr 19 '18 11:04

Krisztián Balla


People also ask

How do I organize imports in TypeScript?

To sort manually, you will need to open the Command Palette ( Ctrl+Shift+P or ⌘+Shift+P ) and find the Sort Imports command. That command can also be used with any custom keybinding ( Ctrl+Shift+O or ⌘+Shift+O are the default).

How do I organize imports in Visual Studio?

In VSCode, go to File -> Preferences -> Settings and click on the icon in the top right hand corner to open up the settings in JSON. Et voilà! Your imports will now be organized every time you save a file.


4 Answers

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!

like image 96
JayKan Avatar answered Oct 17 '22 06:10

JayKan


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

Shift+Alt+O

like image 38
Asif Billa Avatar answered Oct 17 '22 08:10

Asif Billa


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
    }
like image 44
Neil J Avatar answered Oct 17 '22 08:10

Neil J


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.
like image 20
Krisztián Balla Avatar answered Oct 17 '22 07:10

Krisztián Balla