typicode / lowdb

Simple and fast JSON database
MIT License
21.3k stars 918 forks source link

fix: imported subpaths types correctly #569

Closed nulkode closed 1 year ago

nulkode commented 1 year ago

Problem

The problem itself is that in node module resolution, typescript can't import the subpaths' modules or the modules' types. The problem was not the modules but the types, as this works perfectly in Javascript without types. Investigating I found that there's no way to export many type files to include every type file for every subpath. I tried this at first:

"exports": {
    ".": {
      "import": "./lib/index.js",
      "types": "./lib/index.d.ts"
    },
    "./node": {
      "import": "./lib/node.js",
      "types": "./lib/node.d.ts"
    },
    "./browser": {
      "import": "./lib/browser.js",
      "types": "./lib/browser.d.ts"
}

But the types weren't being exported. I tried to find the solution in the typescript documentation, but there's no article regarding subpaths, so I think there's no native support for subpath types.

Solution

Finally, I found this repository that was addressing this particular case. It uses typesVersions to include many types for the other subpaths. It is a bit messy, but it's the only solution I had found for this case.

I ran this solution on these minimum reproduction repositories from #554:

I also run this on my repository of my issue #568 and it solves it too.

So this solution gives compatibility for the node module resolution with typescript.