Skip to content

Auto-importing modules as namespaces? #23830

Closed

Description

I’m not sure what the best way is to fix this, but: auto-importing is incredibly helpful. Alas, it only works for named exports. However, imports such as the following are frequent in Node.js and there, you get no help:

import * as path from 'path';

Possible solution: offer an auto-import if:

  • there is an unknown foo followed by a dot and
  • foo is the base name of a module.

Activity

  1. RyanCavanaugh commented on May 2, 2018

    @RyanCavanaugh
    Member

    Can you clarify what you're typing and when you'd expect this to show up?

  2. rauschma commented on May 2, 2018

    @rauschma
    Author
    • Typing: path. (path being an unknown identifier).
    • Quick fix suggestion: auto-import 'path'.

    But I’m open to anything that helps with import * as: maybe there doesn’t have to be a dot after path. Then auto-importing would be offered for both namespace imports and named imports.

  3. ghost added
    Domain: LS: Completion ListsThe issue relates to showing completion lists in an editor
    on May 2, 2018
  4. ghost added
    SuggestionAn idea for TypeScript
    on May 2, 2018
  5. rauschma commented on May 2, 2018

    @rauschma
    Author

    As a second issue we might consider adding an option to always make a namespace import even if one doesn't already exist.

    I don’t think that that would be useful:

    • With path, both named imports and namespace imports are common.
    • My thinking usually starts with: “something in module path”. Therefore, I’d prefer it if an IDE would namespace-import path for me when I first mention its name.
  6. mhegazy commented on May 3, 2018

    @mhegazy
    Contributor

    So the suggestion here is to offer the names of modules in the completion list with an additional action to import them? i.e. path would show up as a completion entry in the global scope.

  7. rauschma commented on May 3, 2018

    @rauschma
    Author

    Yes! But I’m not sure what you mean by “in the global scope”. path would be global-ish, but the suggestion would appear anywhere, independent of scope.

  8. rauschma commented on May 3, 2018

    @rauschma
    Author

    Oh, and it could be complemented by a quick fix offering to namespace-import path whenever that identifier is ever unknown anywhere.

  9. mhegazy commented on May 3, 2018

    @mhegazy
    Contributor

    My only worry is the amount of unrelated names we put in the completion list.

  10. DanielRosenwasser commented on May 4, 2018

    @DanielRosenwasser
    Member

    My only worry is the amount of unrelated names we put in the completion list.

    True, but we already add in every possible named export in a project context.

  11. added
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    on Nov 6, 2018
  12. 30 remaining items

  13. andrewbranch commented on Feb 8, 2022

    @andrewbranch
    Member

    I’ve looked into this a good bit and decided I don’t want to do it in general. We can’t know the conventions by which the namespace export is named: path comes from "path" but React comes from "react". It would be super annoying when we inevitably get a convention wrong.

    I investigated this:

    This works for path but not for other things like url or querystring. Do you know why?

    The "path" module typings declare an export = path and we pull the name from the declaration of the symbol being exported. On the other hand, the "url" module (along with the majority of node built-ins) has no export = declaration; it’s simply a collection of named exports. These named exports could be wrapped in a namespace named url and then export = url from the top level. The module would behave almost identically to the way it behaves as written today, except that export * from "url" would become an error. It’s worth noting that some of the node built-ins like path and assert are written as an export = out of necessity or convenience, and export * from "path" is already an error because of that, despite it being valid in Node. It could be worth making the change to the rest of the modules to allow auto-imports to work better. This is a question for the Node typings maintainers to consider on DefinitelyTyped.

    Oliver Joseph Ash (@OliverJAsh)’s suggestion of picking up on namespace import names used elsewhere in the program is probably the only solution here that can provide a decent UX, but I think it’s too much complexity for the value it delivers. (It would be too expensive to look up these associations from other files on demand when we’re generating a completions list, so we’d have to introduce a new cache, which means you have to think about cache invalidation.)

    Perhaps VS Code could provide an API so extensions can contribute import suggestions (which appear in the suggestions drop down when typing and as quick fixes), then someone could write an extension to do this. Do you know if anything like that exists?

    As far as I know, these APIs are separate, but there is nothing stopping a single extension from sharing code to contribute to both APIs.

  14. added
    DeclinedThe issue was declined as something which matches the TypeScript vision
    and removed
    RescheduledThis issue was previously scheduled to an earlier milestone
    on Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionDomain: LS: Completion ListsThe issue relates to showing completion lists in an editorDomain: LS: Quick FixesEditor-provided fixes, often called code actions.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions