Typescript import statements
For import statement in typescript it would be nice if we can reference an object in a class (for e,g.) and the IDE can write (when you click on the object reference Foo in example below) an import statement for you. For e.g.
import {Foo} from "../../../bar/somelongpath/foo"
export class Test {
private f: Foo;
}
Webstorm already provides this and it makes a lot of difference (productivitywise) as you dont have to manually type and figure out the path to the import statement.
Should this be a quick fix proposal?
apologies about this. I guess its not. Kindly move to the appropriate area as required. Or let me know if this is something I can do myself. Thanks
@activebiz No worries, you filed the issue in the right spot. I was suggesting a solution to @dbaeumer :)
Nice 1 :)
Since this isn't implemented, is there a better way than having to manually find out how to navigate to the file and typing in the import statement by hand?
is there a better way than having to manually find out how to navigate to the file and typing in the import statement by hand?
We are really interested in that question as well. I have scoured the web and tried various IDEs but seems like Webstorm is the only one that supports this. Would be really nice if VSCode supported it.
How do folks at Microsoft writing lots of Typescript code manage their imports?
+1. I use this feature in WebStorm but really want to work only in VSCode
There is already an issue in the typescript repo for this and it is scheduled for the 2.0 release.
Closing as a dupe of Microsoft/TypeScript#188
@egamma - just for clarity, if the linked issue gets implemented, VS Code will automatically get it as well? This is feature is one the main reasons I still use WebStorm over VS Code.
@egamma - just for clarity, if the linked issue gets implemented, VS Code will automatically get it as well?
this is correct, the completion list shown in VS Code are computed by the TypeScript server that also supports JavaScript.
@egamma Are you referring to only auto-completing as you type import statements? That would be somewhat useful, but the most import thing for me is that if vscode autocompletes a reference to something that is not in scope where it autocompletes, then it adds an import statement to the top of the file for it. I don't feel like I should ever have to type an import statement by hand, with autocomplete help or not.
Agree with @caseyhoward on automatically adding import statements to the top of the file. That's what I was referring to also in my previous comment. Webstorm currently does.
Funny thing is even the Vim plugin seems to do this 😄
https://marketplace.visualstudio.com/items?itemName=steoates.autoimport
@egamma I think you've misunderstood the feature request. From the issue description:
For import statement in typescript it would be nice if we can reference an object in a class (for e,g.) and the IDE can write an import statement for you.
I don't think this is implemented by the TypeScript language service, which supplies autocomplete hints but AFAIK doesn't do anything to modify the source file. Could this please be reopened?
@masaeedu apologies if I misunderstood you.
We have actually enriched the language API in 1.5 so that the language service cannot only provide a single completion but also a set of edits. See https://code.visualstudio.com/updates/v1_5#_extension-authoring the section 'powerful completion items'. We have added this so that this feature can be provided by the C# language service. The same will eventually happen in the TypeScript language service.
That is amazing news! The issue you linked to however (Microsoft/Typescript#188), is not quite what is being asked for. The feature being requested there is better autocomplete while you are writing an import statement. This feature is about not having to explicitly write the import statement at all.
The requested feature is the ability to reference any top level export from any resolvable module (without explicitly importing it), and have the appropriate import statement inserted at the top of the file. This feature can be found in WebStorm, as well as in the Roslyn compilers. See for example:

It would be a huge productivity boost to have something like this in VS Code (or even better, in the language service!). I can confirm that with Visual Studio Code 1.5.3 pointing to tssdk v 2.0.3, I don't see any suggestions for generating an import statement if I reference a top level export from a module I haven't imported yet. E.g. if I install rxjs, don't import it at the top of the file, and try to reference Observable, it would be great if the editor generated import { Observable } from 'rxjs/Observable'.
It is possible that you would need some work on the part of module authors to advertise which of their exports should be advertised for auto-import in this way.
isn't that what the concept of 'export' is all about - exporting something so that others can 'import' it?