Class Alias

Topics: Language Specification
Jul 4, 2013 at 7:39 AM

I was wondering if it will be possible in the future to create class aliases? For example:
static GreeterClass: typeof Greeter;
This way you can have static variables that point to classes, and those classes can be overridden during the program (e.g. by specifying the class in a .ini file and having it changed at program start).

I understand that the problem would be with static variables and functions not being available should the class be overridden by a derived class, but that could be a compile-time warning or a red squiggly?


Jul 8, 2013 at 6:27 PM
It may be possible to do this with a 'typeof' functionality at the type level. Kinda curious, is this for some kind of configurability of the classes in scope? What are you trying to do with it?
Jul 9, 2013 at 3:00 AM
Jul 9, 2013 at 6:26 AM
Edited Jul 9, 2013 at 6:27 AM
The reason being that we have spawners, which are responsible for spawning objects of a particular class.

These spawners can be overridden to spawn different (new) classes by specification in a .ini file, so there is a parameter in the spawner which holds the current configurations' selected class.

It would be nice to be able to specify:
class Spawner {
    static spawnclass: typeof Spawn;
instead of:
class Spawner {
    static spawnclass: new(a: string, b: string, c: { foo(somethingelse: string, anotherparam: int), anotherfoo(someotherparam: string) }, a: someinterface, optional? boolean) => Spawn;
Particularly since we have a number of these spawners (potentially with different parameters) and setting them up is a pain in the head.
Also reading that code is more difficult than it needs to be.

Jul 9, 2013 at 6:06 PM
If you're curious, and don't mind living on the bleeding edge, the current compiler in 'develop' can use the typeof in the type position. This seems to be working with the latest bits:
class Greeter {
  static y: number;
  constructor(public x: number) { }

class Spawner {
  spawnClass: typeof Greeter;

  constructor() {
    this.spawnClass = Greeter;
    var x = new this.spawnClass(3);
Jul 10, 2013 at 12:18 AM
Although I am using Typescript at work, so I'll wait for the VS plugin.

Thanks a lot!