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

[suggestions] Provide import suggestions for imports from package.json "imports" field #49492

Closed
5 tasks done
Jamesernator opened this issue Jun 11, 2022 · 0 comments Β· Fixed by #55015
Closed
5 tasks done
Assignees
Labels
Domain: Auto-import Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@Jamesernator
Copy link

Suggestion

πŸ” Search Terms

imports, suggestions, nodenext

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

So in Node's esm there is the "imports" field which allows aliasing or overriding dependencies based on a host.

It would be nice if TS would suggest these when providing import suggestions, i.e. currently when imports are suggested only the original paths are revealed:

Screenshot from 2022-06-11 12-50-05

However there is a { "imports": { "#base64": "./lib/Base64.js" } } entry in package.json, it would be nice if this would be suggested.

(I made the above as a small example here to demonstrate).

πŸ’» Use Cases

Basically anywhere that people are using "imports" they probably want suggestions from "imports". In fact I'd say that these suggestions should probably come above all other import suggestions (particularly in cases where an "imports" entry and other specifier resolve to the
same module). Why? Well if people are putting things like conditional imports:

{
    "imports": {
        "#assert": {
            "node": "node:assert",
            "browser": "./lib/browserFs.js"
        }
    }
}

Then people would most likely want the version that can be used in either environment rather than the more specific implementations of node:assert or ./lib/browserFs.js (otherwise why would they have bothered having the "imports" to begin with?).

This is true for basic aliases as well, if someone puts an alias in "imports" like my above example, then they presumably want the alias to be the preferred way to import such a file not the original path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Auto-import Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@andrewbranch @RyanCavanaugh @Jamesernator @typescript-bot and others