Categories
TypeScript Answers

How to import all modules from a directory in TypeScript?

Spread the love

Sometimes, we want to import all modules from a directory in TypeScript.

In this article, we’ll look at how to import all modules from a directory in TypeScript.

How to import all modules from a directory in TypeScript?

To import all modules from a directory in TypeScript, we can create a module that reexports all modules that were imported.

For instance, we write

my-module/index.ts

export { default as A } from "./a";
export { default as B } from "./b";

to import the default exports from modules a and b.

Then we can import all the exported modules from my-module/index.ts by writing

import * as whatever from "./my-module";

to import all the exports from my-module/index.ts.

Conclusion

To import all modules from a directory in TypeScript, we can create a module that reexports all modules that were imported.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.