google / playground-elements

Serverless coding environments for the web.
BSD 3-Clause "New" or "Revised" License
549 stars 55 forks source link

Types support for package exports #367

Open augustjk opened 1 year ago

augustjk commented 1 year ago

Current support for @types packages is to just load the base packages into the project. The existing logic handles any dependency types, but is unable to handle types specified by package exports ("exports" field on the package.json).

For example, having the following package.json

{
  "dependencies": {
    "@types/react-dom": "^18"
  }
}

won't produce types for

import {createRoot} from 'react-dom/client';

because only the base @types/react-dom package's index.d.ts is loaded.

It is not able to resolve the ./client part from the package exports here: https://unpkg.com/@types/react-dom@18.2.4/package.json

In this particular case, a work around was available to add @types/react-dom/client as because they thankfully had a file called client.d.ts that the type resolver ended up adding to the project, and along with the package.json file from @types/react-dom, TS was able to resolve this.

Not sure exactly on best approach to support this.. A naive approach is to crawl the "exports" field of package.json and eagerly load all listed types. Or a smarter approach that handles the extra paths.