Ask HN: TypeScript 2 module import – ignore folder levels for external libraries

2 points by fetbaffe ↗ HN
I have two repositories in TypeScript, one project and one library. Both of them have a src directory where all TypeScript files are committed.

I already have npm up and running in the project with the library as a dependency.

I can do from a .ts file in my project's src directory

  import { Foo } from "@vendor/my-lib/src/Foo";
As you can see there is this annoying src directory in the import. Ideally a project should be unaware about the internals of a library, e.g

  import { Foo } from "@vendor/my-lib/Foo";
Can this be solved?

Best would be if the library's tsconfig.json could contain the configuration so that all consumers of the library uses it the same way.

But if that is not possible, the projects tsconfig.json will suffice.

Any hints? Quite hard to find any solid up-to-date documentation for this.

https://www.typescriptlang.org/docs/handbook/module-resolution.html

2 comments

[ 14.8 ms ] story [ 684 ms ] thread
This seems more like a stackoverflow type question, but...

Set the "main" option in the library's package.json to a bundle or index file that exports the public interfaces to your library. Don't expose every source file.

Thanks. Seems like most TypeScript projects creates as index.ts file that just exports whatever is needed and the the compiled index.js is exposed in the "main" option. And if I understand correctly that will give you a index.d.ts that you can refer in the "typings" option in package.json.