Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 447
Andreas Gassmann Avatar asked Dec 01 '19 04:12

Andreas Gassmann


1 Answers

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.

like image 196
Fr3ddyDev Avatar answered Sep 17 '22 14:09

Fr3ddyDev