This project is read-only.

Exporting constructos

Topics: General, Language Specification
Oct 16, 2013 at 10:25 AM
Edited Oct 16, 2013 at 10:25 AM
Is it possible to export constructors? Typically I have one class per module, so find it cumbersome to do this:
import myMod = require('myMod');
var a = new myMod.MyClass();
I'd rather do:
import MyClass= require('MyClass');
var a = new MyClass();
Is this what export default in harmony is for? When is this going to be supported?

Oct 16, 2013 at 2:51 PM
Yes, you can do that with an export assignment:
export = MyClass;
class MyClass {
If the above is in file "MyClass.ts" you can then import and use it with:
import MyClass = require("MyClass");
var a = new MyClass();
Oct 17, 2013 at 9:19 AM
Is it possible to simplify it?
export = class {
Thanks in advance.
Oct 17, 2013 at 6:37 PM
Not with the current design, which requires you to provide a symbol name on the right hand side of the 'export ='.

Using the symbol name is more flexible, as it lets you 'export =' a symbol that is a merged declaration, though I agree that a shorthand could also be convenient in other cases.