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.
You should set the stripInternal
flag in your tsconfig.json
(or from the command line) to true
and use tsdoc comments with the @internal
tag.
// internal/my_file.ts
/** @internal */
export function myInternalFunction() {}
Note that this will remove the function from the type declarations, so it that it will not autocomplete in editors, but if you really need to hide internal details I suggest you bundle your code using Rollup with the TypeScript plugin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With