Tuesday, 1 December 2020

Typescript library: Hide internal exports

I am building a library in typescript that is being published via npm.

In the index.ts, which is the starting point of the library, all the public classes and enums are exported.

import { MyClass } from './internal/my-class'
import { MyEnum } from './internal/my-enum'
export { MyClass, MyEnum }

This works well, but I noticed that users are able to import internal functions/classes etc. as well by using a direct path to the file.

// in a project that installed my library as an npm dependency
import { InternalClass } from 'my-lib/dist/internal/internal-class'

Is there a way to prevent this?

I need to "export" InternalClass because I'm importing and using it throughout my library, but I don't want to expose it publicly. I tried using namespaces, but couldn't really get it to work.



from Typescript library: Hide internal exports

No comments:

Post a Comment