Workaround for Node.js subpath exports in TypeScript
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
Use typesVersions
to workaround.
{
"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"
You can install and try this package.
$ npm i typescript-subpath-exports-workaround
MIT