typescripttypescript-typingstypescript-declarations

How to convert typescript project to node package


I have an older typescript project that exports a lot of functions and interfaces in multiple files. Dependent packages pulled in those exports by linking directly to the file in the directory.

I am trying to convert the dependency to a true npm package, with an index.js and index.d.js, but struggling with how. I don't know what those two files should look like.

For example, I need to export both a type HttpHandler and an implementation httpHandler

For this package, there is no default.

Examples?


Solution

  • Below is an example library @ed4becky/acme. The library's source code is in the src directory, and a fake consumer (fake-external-consumer.ts). In a real world example, fake-exernal-consumer.ts would be in its own repo/project, I just kept it outside the src for as a visual indictor and for simplicity purposes. In the example you can see how I created the library @ed4becky/acme, and how it's imported import { A, B } from @ed4becky/acme. I'm able to import from the library because class A and B are exported in src/index.ts.

    https://codesandbox.io/s/mystifying-gauss-b8zjk