Is it possible to overload function type member variable?

Topics: General
Feb 14, 2014 at 2:48 AM
Edited Feb 14, 2014 at 2:51 AM
I want code like this can be compiled:
class A
{
  foo:(a:number)=>void;
  foo:(b:string)=>void;
  constructor()
  {
    var xxx = 1;
    var yyy = "";
    this.foo = function(aorb){
        if (typeof aorb == "string") yyy = aorb;
        else xxx = aorb;
    }
  }
}
or may I declare member function in class,just declare not implement;
class A
{
  declare foo(a:number):void;
  declare foo(b:string):void;

  constructor()
  {
    var xxx = 1;
    var yyy = "";
    this.foo = function(aorb){
        if (typeof aorb == "string") yyy = aorb;
        else xxx = aorb;
    }
  }
}
Coordinator
Feb 14, 2014 at 4:25 PM
Yes, but you need to use the desugared syntax for an overload:
class A
{
  foo: {
      (a:number): void;
      (b:string): void;
  }
  constructor()
  {
    var xxx = 1;
    var yyy = "";
    this.foo = function(aorb){
        if (typeof aorb == "string") yyy = aorb;
        else xxx = aorb;
    }
  }
}