Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there auto-import functionality for typescript in Visual Studio Code?

Is there any shortcut that will allow me to auto generate my typescript imports? Like hitting ctrl+space next to the type name and having the import declaration placed at the top of the file. If not, what about intellisense for filling out the module reference path so that I wont have to do it manually? I would really love to use vscode but having to do manual imports for typescript is killing me.

like image 980
ify.io Avatar asked May 03 '15 14:05

ify.io


People also ask

Does Vscode support TypeScript?

Visual Studio Code includes TypeScript language support but does not include the TypeScript compiler, tsc . You will need to install the TypeScript compiler either globally or in your workspace to transpile TypeScript source code to JavaScript ( tsc HelloWorld. ts ). You can test your install by checking the version.

Does Visual Studio support TypeScript?

TypeScript supportBy default, Visual Studio 2022 provides language support for JavaScript and TypeScript files to power IntelliSense without any specific project configuration. For compiling TypeScript, Visual Studio gives you the flexibility to choose which version of TypeScript to use on a per-project basis.


2 Answers

This has just been released in version 1.18.

From the release notes:

Auto Import for JavaScript and TypeScript

Speed up your coding with auto imports for JavaScript and TypeScript. The suggestion list now includes all exported symbols in the current project. Just start typing:

Global symbols are now shown in the suggestion list

If you choose one of the suggestion from another file or module, VS Code will automatically add an import for it. In this example, VS Code adds an import for Hercules to the top of the file:

After selecting a symbol form a different file, an import is added for it automatically

Auto imports requires TypeScript 2.6+. You can disable auto imports by setting "typescript.autoImportSuggestions.enabled": false.

like image 121
flipchart Avatar answered Oct 11 '22 23:10

flipchart


I believe the plugin called "TypeScript Importer" does exactly what You mean: https://marketplace.visualstudio.com/items?itemName=pmneo.tsimporter .

Automatically searches for TypeScript definitions in workspace files and provides all known symbols as completion item to allow code completion.

enter image description here

With it You can truly use Ctrl+Space to choose what exactly You would like to be imported.

You can find and install it from Ctrl+Shift+X menu or just by pasting ext install tsimporter in Quick Open menu opened with Ctrl+P.

like image 34
kcpr Avatar answered Oct 11 '22 23:10

kcpr