Skip to content

Workaround for Node.js subpath exports in TypeScript

Notifications You must be signed in to change notification settings

teppeis/typescript-subpath-exports-workaround

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Workaround for Node.js subpath exports in TypeScript

Topics

Resources

Stars

Watchers

Forks