I am dynamically loading modules based on which are present in a folder. Basically I'm iterating over some folders and calling require
, saving the result in a Map.
E.g., the loader does this:
const factories = new Map<string, Object>();
:
: foreach loop looking for valid dirs...
:
const obj = require(`${__dirname}/${dir.name}/index.ts`);
factories.set(dir.name, obj);
Cool, this works great!
Each factory index.ts
above exports a singleton of a class:
export default{
factory: new X_Factory()
};
Also works, woo.
Then later on I attempt to call a function that exists on in the factory class:
const y: Object|undefined = factories.get('dynamic_module_name');
// @ts-ignore noImplicitAnyForClassMembers
y.default.factory.sync();
grr
If I remove the ts-ignore, I have no idea how to make TypeScript happy.
Suggestions? I prefer to not hack my way out of this. I tried interfaces and using Module, NodeModule and NodeRequire instead of Object, but that doesn't capture the fact that the module has a specific interface (I also tried to create an interface, but couldn't get it to work).
I thought this answer might be useful, but I'm not sure it is the same TS.