Skip to content

Types that are found, but not exported in package.json, should still be usable #52363

Closed
samhirtarif/react-audio-visualize
#20
@justinfagnani

Description

@justinfagnani

Suggestion

With module resolution "Node16" or "NodeNext" I'm running into problems using packages where tsc can find declaration files but won't use them because they're not exported in package.json.

The error I get is:

error TS7016: Could not find a declaration file for module '@web/dev-server'. '/Users/justin/Projects/Web/simple-story-viewer/node_modules/@web/dev-server/index.mjs' implicitly has an 'any' type.
  There are types at '/Users/justin/Projects/Web/simple-story-viewer/node_modules/@web/dev-server/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@web/dev-server' library may need to update its package.json or typings.

This means that I can't use the package without copying it's types or patching the package locally with something like patch-package.

I know the package should technically be exporting its types correctly (though I might argue that this is being overly pedantic and that the existence of the these specific files strongly implies that the author intended them to be used), but as a user of TypeScript I'm stuck and tsc seems to have enough information to just laod the types for me so I can at least make progress.

I'd like some sort of option to tell tsc to loosen up about these files. If there's an encapsulation violation or something breaks with the next version of the package I'm using, I'll deal with it.

It's also possible that behavior is a bug. As commented on by @andrewbranch in #46334:

a foolproof way to write a valid project structure and package.json file is just to publish your declaration files in the same place as their partner JS files and literally never write types anywhere in your package.json.

This seems to say that if I'm importing index.mjs and there's an index.d.ts next to it, the types should load. Is that correct?

🔍 Search Terms

  • package export types
  • this result could not be resolved when respecting package.json "exports"

A lot of issues came up in the first search. I could have missed a duplicate.

✅ 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

An option to load types not exported in packae.json.

📃 Motivating Example

https://github.com/modernweb-dev/web/blob/master/packages/dev-server/package.json

💻 Use Cases

Packages that aren't ready for TypeScript's strict type loading behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExternalRelates to another program, environment, or user action which we cannot control.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions