Export Members from a TypeScript NPM Package
The short memo on how to conveniently export members of a TypeScript NPM package.
Context
Let us export members of a TypeScript package in the index.ts
file to be accessible by package consumers with namespaces, aliases or rename.
[!WARNING] The namespaced exports somehow break
eslint
detects TypeScript types of the imported package members. Will see in practice how it reveals.In the concrete case below it could come from either 1) the import and export names were the same in direct
export
and the namespacedexport
or 2) just because namespaced export types were not recognized byeslint
.
The examples of an npm package members exports (usually from index.ts
file).
Beforehand set "module": "NodeNext"
and "moduleResolution": "NodeNext"
in tsconfig.json
compilerOptions
settings.
// index.ts
'use strict';
/**
* Export one line with no need preceding import.
*/
export { EHTTPMethods,
default as HTTPMethodsConvenience, type TCustomHTTPMethodsConstraint
} from './core/methods/HTTPMethodsConvenience.js';
/**
* Export under a namespace.
* Note the trick: members are aliased at import with `_` prefix and "de-aliased" back to
* the original names at export.
*/
import {
EHTTPMethods as _EHTTPMethods,
default as _HTTPMethodsConvenience,
type TCustomHTTPMethodsConstraint as _TCustomHTTPMethodsConstraint
} from './core/methods/HTTPMethodsConvenience.js';
export namespace HTTPConveniencePack {
export import EHTTPMethods = _EHTTPMethods;
export type TCustomHTTPMethodsConstraint = _TCustomHTTPMethodsConstraint;
export class HTTPMethodsConvenience extends _HTTPMethodsConvenience { };
}