vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Typescript import statements

Open activebiz opened this issue 11 years ago • 36 comments

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.

activebiz avatar Nov 30 '15 13:11 activebiz

Should this be a quick fix proposal?

alexdima avatar Nov 30 '15 13:11 alexdima

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 avatar Nov 30 '15 13:11 activebiz

@activebiz No worries, you filed the issue in the right spot. I was suggesting a solution to @dbaeumer :)

alexdima avatar Nov 30 '15 15:11 alexdima

Nice 1 :)

activebiz avatar Nov 30 '15 15:11 activebiz

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?

caseyhoward avatar Jan 14 '16 01:01 caseyhoward

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?

atifsyedali avatar Mar 02 '16 20:03 atifsyedali

+1. I use this feature in WebStorm but really want to work only in VSCode

pivalig avatar Mar 04 '16 18:03 pivalig

There is already an issue in the typescript repo for this and it is scheduled for the 2.0 release.

frederikschubert avatar Mar 04 '16 19:03 frederikschubert

Closing as a dupe of Microsoft/TypeScript#188

egamma avatar Mar 07 '16 09:03 egamma

@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.

dfaivre avatar May 10 '16 19:05 dfaivre

@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 avatar May 11 '16 14:05 egamma

@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.

caseyhoward avatar Aug 05 '16 16:08 caseyhoward

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.

atifsyedali avatar Aug 05 '16 18:08 atifsyedali

Funny thing is even the Vim plugin seems to do this 😄

lufzle avatar Sep 18 '16 22:09 lufzle

https://marketplace.visualstudio.com/items?itemName=steoates.autoimport

tonymk avatar Sep 19 '16 07:09 tonymk

@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 avatar Sep 30 '16 08:09 masaeedu

@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.

egamma avatar Sep 30 '16 11:09 egamma

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:

Visual Studio offering to complete missing imports

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'.

masaeedu avatar Sep 30 '16 12:09 masaeedu

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.

masaeedu avatar Sep 30 '16 12:09 masaeedu

isn't that what the concept of 'export' is all about - exporting something so that others can 'import' it?

sophistyx avatar Oct 12 '16 13:10 sophistyx