1.0.1 • Published 3 years ago

typescript-subpath-exports-workaround v1.0.1

Weekly downloads
22
License
MIT
Repository
github
Last release
3 years ago

typescript-subpath-exports-workaround

Workaround for Node.js subpath exports in TypeScript

npm version license

Why?

Node.js v12+ extends package entry points features like subpath exports or conditional exports but TypeScript doesn't support them yet.

Support for NodeJS 12.7+ package exports · Issue #33079 · microsoft/TypeScript · GitHub

How?

Use typesVersions to workaround.

package.json

{
  "main": "dist/index.js",
  "types": "dist-types/index.d.ts",
  "exports": {
    ".": "./dist/index.js",
    "./exported": "./dist/exported.js"
  },
  "typesVersions": {
    "*": {
      "exported": ["dist-types/exported"]
    }
  }
}

Users can import only the package root and the exported subpath /exported.

// Pass
import "typescript-subpath-exports-workaround"
import "typescript-subpath-exports-workaround/exported"

// Error
import "typescript-subpath-exports-workaround/not-exported"
import "typescript-subpath-exports-workaround/dist/exported"
import "typescript-subpath-exports-workaround/dist/not-exported"

Demo

You can install and try this package.

$ npm i typescript-subpath-exports-workaround

License

MIT