How to import all modules from a directory in TypeScript?

No this is not possible. What most people do is create an index.js file which re-exports all files in the same directory.

Example:

my-module/
  a.ts
  b.ts
  index.ts

a.ts

export default function hello() {
  console.log("hello");
}

b.ts

export default function world() {
  console.log("world");
}

index.ts

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

You can use the * character to re-export every export of a module with a single line. Be aware that TypeScript will error if a member with the same name has already been exported though (thanks to @haysclark for the tip).

export * from "./somePath";

The index name can be dropped (same as in javascript):

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

console.log(whatever);
// Logs: { A: [Function: hello], B: [Function: world] }

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)