Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS auto import should support configuring whether a star or a qualified import is used. #19630

Open
mjbvz opened this issue Nov 1, 2017 · 13 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Nov 1, 2017

From @dbaeumer on October 31, 2017 11:29

Testing: #37177

  • vscode source code opened in VS Code
  • open a file that doesn't import 'vs/base/common/types'
  • type isStringArray
  • select the entry from code complete list
  • the following import is inserted:
import { isStringArray } from 'vs/base/common/types';

However what I want in this case is

import * as Types from 'vs/base/common/types';

and the code should become Types.isStringArray

Would be cool if I can control this via a setting.

Copied from original issue: microsoft/vscode#37258

@mjbvz mjbvz self-assigned this Nov 1, 2017
@mjbvz mjbvz added VS Code Tracked There is a VS Code equivalent to this issue and removed typescript labels Nov 1, 2017
@mjbvz mjbvz removed their assignment Nov 1, 2017
@mjbvz
Copy link
Contributor Author

mjbvz commented Nov 1, 2017

I think this one needs some thought. For example, what name should we use for * from X?

@ghost
Copy link

ghost commented Nov 1, 2017

Maybe this would be better as a refactoring? Change named imports to namespace import. We could also do the other way around.
Otherwise we would double the size of the already enormous completion list.

For example, what name should we use for * from X?

Refactorings conveniently already come with a rename location.

@DanielRosenwasser
Copy link
Member

Maybe this would be better as a refactoring?

See #19260

@dbaeumer
Copy link
Member

dbaeumer commented Nov 1, 2017

Having that as a separate refactoring makes sense, nevertheless I would like to have this right in the first place and not me picking an entry from a completion list and then being forced to execute another refactorings. In another tool I worked on ( :-) ) we had a configuration comparable to this to control the import generation:

{
    module: 'vs/base/common/types',
    importType: ImportType.Star,
    identifier: 'Types'
}

Default would IMO still be a qualified import.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 1, 2017

Default would IMO still be a qualified import.

you mean import *? curious why?

@mhegazy mhegazy added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Nov 2, 2017
@dbaeumer
Copy link
Member

dbaeumer commented Nov 3, 2017

@mhegazy sorry used wrong words. Default should be import { .... } from

@octref
Copy link

octref commented May 11, 2018

Copying my original problem here:

For example, now if I type resolve, VS Code auto suggests millions of resolve from all libraries. This is not helpful:

image

For me, I usually have this

import * as path from 'path'

path.resolve(...)

However now path. doesn't have any auto import associated with suggestions.

I hope path. can generate completion items that when accepted, add automatic imports to top.
The expected behavior is:

  • Type path.
  • VS Code should auto complete, based on either type definition at @types/node or the Automatic Type Acquisition
  • When user accepts path.resolve, add to top of file import * as path from 'path'

I think if one types resolve, auto import import { resolve } from 'path' makes sense. However when one types path.resolve, auto import should do import * as path from 'path'.

@jasonkuhrt
Copy link

jasonkuhrt commented Jun 20, 2018

In my experience Qualified import makes for more readable code in most cases and its the import pattern advocated for in Haskell, Python, and language-enforced in Go. Likely other language too, I just don't know. Looking forward to support for this. I think its unfortunate qualified is not the default but neither is strict mode etc. so its just another flag to add the many already needed for a solid baseline in TS... not a huge deal I guess. Though its a missed opportunity to nudge the community in a better direction in my honest personal opinion :)

@OliverJAsh
Copy link
Contributor

Related: #23830

@katis
Copy link

katis commented Nov 12, 2019

I would be happy if I could write an item name, find it in the completion list, press <SOME_MODIFIER>+Tab/Enter and have it import the thing as qualified. You could pick the name from file/module the item belongs to, with a kebab-/snakecase converted to camelcase. Rename refactoring could be then used on the qualified name if needed.

i.e.

getName completion could become:

import * as User from `some/module/User`

...

User.getName

@JuoCode
Copy link

JuoCode commented Mar 9, 2023

I think this one needs some thought. For example, what name should we use for * from X?

Consider https://jsdoc.app/tags-module.html. We can define the @module tags in the single file or the index.ts under the directory, and then, it should be warned if there are multiple different @module under a directory with index.ts

@wildeyes
Copy link

Anyone know of an extension that can do this right now?

@OliverJAsh
Copy link
Contributor

@wildeyes You might find this useful: https://github.com/unsplash/ts-namespace-import-plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue
Projects
None yet
Development

No branches or pull requests

10 participants