Is there a way to dynamically load classes in TypeScript without using a default export method? I have managed to make it work partly, but I am looking for a solution that doesn't require a default export:
export default class Test extends Base {
...
register(new (await import(file)).default())
Edit: Thanks to Dhananjai's suggestion, I discovered this alternative approach: (parse is coming from path and I am using glob to get all paths to files in a directory)
for (const file of files) {
try {
const { name } = parse(file)
const command = new (await import(file))[name]()
this.register(command)
} catch {
return logger.error(`${file} is either not exporting a class or the file name does not match the exported class name`)
}
}