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

Type imports not taking "exports" of package.json into account #49971

Closed
KnorpelSenf opened this issue Jul 20, 2022 · 2 comments
Closed

Type imports not taking "exports" of package.json into account #49971

KnorpelSenf opened this issue Jul 20, 2022 · 2 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@KnorpelSenf
Copy link

Bug Report

The LSP sometimes suggests the wrong paths from which to import types, ignoring the library's "exports" field defined in package.json. These imports should neither be allowed nor suggested.

🔎 Search Terms

type import auto-complete exports package.json

🕗 Version & Regression Information

As far as I know, this issue has always existed, since TS starting supporting exports fields.

⏯ Playground Link

Playground link with relevant code

💻 Code

Let's say that the library's package.json file contains this:

{
    "name": "foo",
    // ...
    "exports": {
        "./types": {
            "types": "./out/types.d.ts"
        },
        ".": {
            "types": "./out/index.d.ts",
            "default": "./out/index.js"
        }
    }
}

Note how the library ships its own code along with type annotations for that, as well as some other manually created helper type definitions in a different file. The former can be imported from foo while the helper types can be imported form foo/types.

For instance, it is possible to import the type Bar from foo/types.

import { type Bar } from 'foo/types'

type X = Bar

🙁 Actual behavior

When I use the type Bar in my code, the LSP suggests several different ways to add an import statement for that type. However, even though the type Bar can only be imported from foo/types (note how no other directory is exposed by the lib), it suggests lots of file paths that all don't make sense.

Here a screenshot where this appears with a library I am maintaining:
screenshot
Note how the correct path grammy/types is not even among the suggestions. However, if I use it, it will still work correctly.

It seems like TS allows to import types from paths that are not exported. In contrast, if you try to import things from grammy/out, it will throw an error at runtime.

It also seems like TS does not always suggest the correct paths, or at least the suggestions are not comprehensive.

🙂 Expected behavior

I would expect TS to also respect the exports field in package.json when importing types. That way, users of my library will use the correct import paths to the public interface of the package, rather than rolling with the suggestions from autocomplete which result in type imports to internal files inside the library.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jul 20, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.9.0 milestone Jul 20, 2022
@andrewbranch
Copy link
Member

@KnorpelSenf what are your tsconfig settings? TypeScript only supports exports in --moduleResolution node16 or nodenext.

@KnorpelSenf
Copy link
Author

TypeScript only supports exports in --moduleResolution node16 or nodenext.

I see, I was not aware of that. When I change the module resolution, it works as expected.
image
Thanks for clarifying!

nix6839 added a commit to nix6839/prettier-config that referenced this issue Aug 14, 2023
TypeScript only supports `exports` in `--moduleResolution node16` or `nodenext`.
See <microsoft/TypeScript#49971 (comment)>
nix6839 added a commit to nix6839/prettier-config that referenced this issue Aug 14, 2023
TypeScript only supports `exports` in `--moduleResolution node16` or `nodenext`.
See <microsoft/TypeScript#49971 (comment)>
nix6839 added a commit to nix6839/prettier-config that referenced this issue Aug 21, 2023
TypeScript only supports `exports` in `--moduleResolution node16` or `nodenext`.
See <microsoft/TypeScript#49971 (comment)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

3 participants