1

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?

4
  • It sounds like you're trying to accomplish this: itnext.io/… Commented Feb 22, 2020 at 15:47
  • except; all the examples show putting the code in index.js and the types in index.d.ts. I already have code in multiple files like src/a.ts and src/b.ts. I don't know how to make them visible or as a module Commented Feb 22, 2020 at 15:52
  • 1
    Create a index.ts file, then add export * from './src/a'; and export * from './src/b';. Commented Feb 22, 2020 at 15:54
  • Paul, you want to put that in an answer, because you nailed it! Commented Feb 22, 2020 at 15:59

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I had to make one correction to the existing code to get this work. The method I was using as mu guinea pig was defined "const a1 = " then at the bottom export default a1; I changed it to export const a1 and removed the default. Thanks for quick response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.