#Is there a way to automatically export TypeScript modules like in Node.JS?

4 messages · Page 1 of 1 (latest)

blazing dew
#

I have a large number of TypeScript modules that I need to export in a specific way so that their types are also available without errors, similar to how it's done in Node.JS. Here is an example of how I'm currently doing it:

export * from './clans';
export * from './clans.dto';

export * from './defferedActions';
export * from './defferedActions.dto';

export * from './eventRooms';
export * from './eventRooms.dto';

...

export const list = [
        'clans', 'defferedActions', 'eventRooms', ...
];

In NodeJS, I could use require to automatically export modules, but this approach does not work in TypeScript. Here's an example of how I would do it in NodeJS:

const fs = require('fs');
const path = require('path');

const modulesDir = path.join(__dirname, 'modules');
const modules = fs.readdirSync(modulesDir);

modules.forEach((module) => {
    if (module.endsWith('.js')) {
        const name = module.slice(0, -3);
        module.exports[name] = require(path.join(modulesDir, module));
    }
});

const list = modules.map((module) => module.slice(0, -3));

I'm looking for suggestions on how to automatically export my TypeScript modules so that their types are also available without errors.

willow night
#

Just export them manually
Imports from dynamic file paths aren't analyzable by TS

blazing dew
#

Okay, I just thought there might be a way to do this dynamically.

#

!resolved