I didn't find a way to export a class instance easily in TypeScript. I had to come up with the following workaround to generate proper javascript code.
var expo = new Logger("default");
export = expo;
generates
var expo = new Logger("default");
module.exports = expo;
Is there an easier way of achieving this?
Use named exports to export multiple classes in TypeScript, e.g. export class A {} and export class B {} . The exported classes can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a file.
TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.
The "Module has no default export" error occurs when we try to import as default from a module that doesn't have a default export. To solve the error make sure the module has a named export and wrap the import in curly braces, e.g. import {myFunction} from './myModule' .
Quite by accident, I found this way to export an instance:
class MyClass(){}
export default new MyClass();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With