Skip to content

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

Closed
@Jamesernator

Description

@Jamesernator

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions