Importing classes anonymously

Topics: General
Oct 2, 2012 at 8:23 PM
Edited Oct 2, 2012 at 8:24 PM

Let's say I have the following class definition in base.ts:

export class Base {
...
}

And in derived.ts, I define the following class:

import Base = module("base");

export class Derived extends Base.Base {
...
}

If i define all classes in separate files, then I have to import plenty of modules, all with different names. In this example, I named the module "Base", so in order to access the imported class "Base", I have to type "Base.Base". Instead, I'd like to be able to import classes anonymously, so I can access an imported class by its name alone instead of having to do "Module.Class". Is this possible?

Oct 2, 2012 at 11:30 PM
Edited Oct 2, 2012 at 11:33 PM

It gets even worse when I declare my class inside a module, e.g. to simulate namespaces:

export Module {
	export class Base { 
		...
	}
}

Now I have to access the exported class like this:

import Base = module("base"); 

export class Derived extends Base.Module.Base { 
	...
}

Is there no way around this?

Thank you!

Oct 3, 2012 at 11:59 AM

Well, this works:

/// <reference path="./base.ts" />

Now I can reference class Base directly. However, it only works if I remove the export keyword. Why is that?

Oct 4, 2012 at 1:03 AM

See my reply to your other thread at http://typescript.codeplex.com/discussions/397862.  When you use import you are using a different module system entirely. I doubt you want to factor every class into a separate external module.