├── .gitignore ├── README.md ├── dist-types ├── exported.d.ts ├── index.d.ts └── not-exported.d.ts ├── dist ├── exported.js ├── index.js └── not-exported.js ├── package-lock.json ├── package.json ├── src ├── exported.ts ├── index.ts └── not-exported.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # typescript-subpath-exports-workaround 2 | 3 | Workaround for Node.js subpath exports in TypeScript 4 | 5 | [![npm version][npm-image]][npm-url] 6 | ![license][license] 7 | 8 | 9 | ## Why? 10 | 11 | Node.js v12+ extends [package entry points features](https://nodejs.org/api/packages.html#packages_package_entry_points) like [subpath exports](https://nodejs.org/api/packages.html#packages_subpath_exports) or [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports) but TypeScript doesn't support them yet. 12 | 13 | > [Support for NodeJS 12.7+ package exports · Issue #33079 · microsoft/TypeScript · GitHub](https://github.com/microsoft/TypeScript/issues/33079) 14 | 15 | ## How? 16 | 17 | Use [`typesVersions`](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#version-selection-with-typesversions) to workaround. 18 | 19 | #### package.json 20 | 21 | ```json 22 | { 23 | "main": "dist/index.js", 24 | "types": "dist-types/index.d.ts", 25 | "exports": { 26 | ".": "./dist/index.js", 27 | "./exported": "./dist/exported.js" 28 | }, 29 | "typesVersions": { 30 | "*": { 31 | "exported": ["dist-types/exported"] 32 | } 33 | } 34 | } 35 | ``` 36 | 37 | Users can import only the package root and the exported subpath `/exported`. 38 | 39 | ```ts 40 | // Pass 41 | import "typescript-subpath-exports-workaround" 42 | import "typescript-subpath-exports-workaround/exported" 43 | 44 | // Error 45 | import "typescript-subpath-exports-workaround/not-exported" 46 | import "typescript-subpath-exports-workaround/dist/exported" 47 | import "typescript-subpath-exports-workaround/dist/not-exported" 48 | ``` 49 | 50 | ## Demo 51 | 52 | You can install and try this package. 53 | 54 | ```console 55 | $ npm i typescript-subpath-exports-workaround 56 | ``` 57 | 58 | ## License 59 | 60 | MIT 61 | 62 | [npm-image]: https://badgen.net/npm/v/typescript-subpath-exports-workaround?icon=npm&label= 63 | [npm-url]: https://npmjs.org/package/typescript-subpath-exports-workaround 64 | [npm-downloads-image]: https://badgen.net/npm/dm/typescript-subpath-exports-workaround 65 | [ts-version]: https://badgen.net/badge/typescript/%3E=4.1?icon=typescript 66 | [license]: https://badgen.net/npm/license/typescript-subpath-exports-workaround 67 | -------------------------------------------------------------------------------- /dist-types/exported.d.ts: -------------------------------------------------------------------------------- 1 | export declare function foo(): string; 2 | -------------------------------------------------------------------------------- /dist-types/index.d.ts: -------------------------------------------------------------------------------- 1 | export declare function hello(): string; 2 | -------------------------------------------------------------------------------- /dist-types/not-exported.d.ts: -------------------------------------------------------------------------------- 1 | export declare function bar(): string; 2 | -------------------------------------------------------------------------------- /dist/exported.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | exports.foo = void 0; 4 | function foo() { 5 | return "foo!"; 6 | } 7 | exports.foo = foo; 8 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | exports.hello = void 0; 4 | function hello() { 5 | return "hello!"; 6 | } 7 | exports.hello = hello; 8 | -------------------------------------------------------------------------------- /dist/not-exported.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | exports.bar = void 0; 4 | function bar() { 5 | return "bar!"; 6 | } 7 | exports.bar = bar; 8 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typescript-subpath-exports-workaround", 3 | "version": "1.0.1", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "typescript-subpath-exports-workaround", 9 | "version": "0.1.0", 10 | "license": "MIT", 11 | "devDependencies": { 12 | "typescript": "^4.1.3" 13 | } 14 | }, 15 | "node_modules/typescript": { 16 | "version": "4.1.3", 17 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz", 18 | "integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==", 19 | "dev": true, 20 | "bin": { 21 | "tsc": "bin/tsc", 22 | "tsserver": "bin/tsserver" 23 | }, 24 | "engines": { 25 | "node": ">=4.2.0" 26 | } 27 | } 28 | }, 29 | "dependencies": { 30 | "typescript": { 31 | "version": "4.1.3", 32 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz", 33 | "integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==", 34 | "dev": true 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typescript-subpath-exports-workaround", 3 | "version": "1.0.1", 4 | "description": "Workaround for Node.js subpath exports in TypeScript", 5 | "main": "dist/index.js", 6 | "types": "dist-types/index.d.ts", 7 | "scripts": { 8 | "build": "tsc", 9 | "clean": "rm -rf dist dist-types", 10 | "test": "npm run clean && npm run build" 11 | }, 12 | "exports": { 13 | ".": "./dist/index.js", 14 | "./exported": "./dist/exported.js" 15 | }, 16 | "typesVersions": { 17 | "*": { 18 | "exported": [ 19 | "dist-types/exported" 20 | ] 21 | } 22 | }, 23 | "files": [ 24 | "dist", 25 | "dist-types" 26 | ], 27 | "keywords": [ 28 | "typescript", 29 | "node.js" 30 | ], 31 | "homepage": "https://github.com/teppeis/typescript-subpath-exports-workaround", 32 | "repository": "teppeis/typescript-subpath-exports-workaround", 33 | "author": "Teppei Sato ", 34 | "license": "MIT", 35 | "devDependencies": { 36 | "typescript": "^4.1.3" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/exported.ts: -------------------------------------------------------------------------------- 1 | export function foo() { 2 | return "foo!"; 3 | } 4 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export function hello() { 2 | return "hello!"; 3 | } 4 | -------------------------------------------------------------------------------- /src/not-exported.ts: -------------------------------------------------------------------------------- 1 | export function bar() { 2 | return "bar!"; 3 | } 4 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Visit https://aka.ms/tsconfig.json to read more about this file */ 4 | 5 | /* Basic Options */ 6 | // "incremental": true, /* Enable incremental compilation */ 7 | "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ 8 | "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ 9 | // "lib": [], /* Specify library files to be included in the compilation. */ 10 | // "allowJs": true, /* Allow javascript files to be compiled. */ 11 | // "checkJs": true, /* Report errors in .js files. */ 12 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 13 | "declaration": true, /* Generates corresponding '.d.ts' file. */ 14 | "declarationDir": "./dist-types", 15 | // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ 16 | // "sourceMap": true, /* Generates corresponding '.map' file. */ 17 | // "outFile": "./", /* Concatenate and emit output to single file. */ 18 | "outDir": "./dist", /* Redirect output structure to the directory. */ 19 | "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 20 | // "composite": true, /* Enable project compilation */ 21 | // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ 22 | // "removeComments": true, /* Do not emit comments to output. */ 23 | // "noEmit": true, /* Do not emit outputs. */ 24 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 25 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 26 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 27 | 28 | /* Strict Type-Checking Options */ 29 | "strict": true, /* Enable all strict type-checking options. */ 30 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 31 | // "strictNullChecks": true, /* Enable strict null checks. */ 32 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 33 | // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ 34 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ 35 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 36 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 37 | 38 | /* Additional Checks */ 39 | // "noUnusedLocals": true, /* Report errors on unused locals. */ 40 | // "noUnusedParameters": true, /* Report errors on unused parameters. */ 41 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 42 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 43 | // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ 44 | 45 | /* Module Resolution Options */ 46 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 47 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ 48 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 49 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 50 | // "typeRoots": [], /* List of folders to include type definitions from. */ 51 | // "types": [], /* Type declaration files to be included in compilation. */ 52 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 53 | "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 54 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 55 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 56 | 57 | /* Source Map Options */ 58 | // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 59 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 60 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 61 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 62 | 63 | /* Experimental Options */ 64 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 65 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 66 | 67 | /* Advanced Options */ 68 | "skipLibCheck": true, /* Skip type checking of declaration files. */ 69 | "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ 70 | } 71 | } 72 | --------------------------------------------------------------------------------