Typescript doesn't allow for more than one constructor(each with different parameters)

Topics: Language Specification
Oct 2, 2012 at 8:23 AM

Why? Could this feature be added in the future?

Oct 2, 2012 at 9:02 AM
Edited Oct 2, 2012 at 9:17 AM

Also it seems that method overloading does not work either...

Oct 2, 2012 at 11:45 AM

In TypeScript you can have a few declarations, but only one implementation of functions/constructors

function f(x: number): number; // declaration

function f(x: string): string; // declaration

function f(x: any): any { return x; } // impementation

 

f(10) // 10

f("str") // str

f({a : 10}) // compile time error

Developer
Oct 2, 2012 at 11:54 AM

You can have multiple constructor or method signatures, but only one implementation. For example:

function foo(s: string): string;
function foo(n: number): number;
function foo(x: any): any {
    return x;
}

It is up to the implementation of the function to ensure it actually delivers a result that matches the overload signatures. The compiler does not validate this.

Oct 8, 2012 at 10:03 PM

Is there any plan to improve this support? Is this by design? Why not real overloads with implementations?

Oct 9, 2012 at 10:39 AM
ursuletzu wrote:

Why not real overloads with implementations?

How would you compile that into javascript ?

Correct me if i'm wrong but i don't think javascript support real overloading (same function identifier, multi signatures with different implementations).

It would be possible to "simulate" this behavior but at the price of performance overheads : http://ejohn.org/blog/javascript-method-overloading/

Oct 9, 2012 at 11:49 AM
Edited Oct 9, 2012 at 11:50 AM

In the case of method overloading, suffixes could be added to different overloads:

function foo(s: string): string -> function foo_string_string(s)
function foo(n: number): number -> function foo_number_number(n)

Wouldn't work for overloaded constructors, though.

Oct 9, 2012 at 12:24 PM

I would love to see the compiler doing this kind of renaming when js is generated...

Oct 9, 2012 at 12:40 PM
Edited Oct 9, 2012 at 12:45 PM
vexator wrote:

In the case of method overloading, suffixes could be added to different overloads:

function foo(s: string): string -> function foo_string_string(s)
function foo(n: number): number -> function foo_number_number(n)

Wouldn't work for overloaded constructors, though.

Correct me if i'm wrong, in the generated javascript implementation, each overload calls would had to evaluate which args has been provided and conditionaly (switch/if) call the corresponding generated method.

Is it worth the function call overhead ? Take that into consideration in high performance situations.

I mean, if you decide to implement it that way in your code, it's your choice. But if the TS compiler implements this pattern by design, it's another story.

Oct 9, 2012 at 12:47 PM
Edited Oct 9, 2012 at 12:47 PM

why not:

 

class Foo
{
      show(s: string): void {.. } 
      //compiler will transform it to show1(s);

      show(s : number): void { ... }
      //compiler will transform it to show2(s);
}

var foo = new Foo();

foo.show('a'); // will render in js  foo.show1('a');
foo.show(1);   // will render in js": foo.show2(1);
Oct 9, 2012 at 12:55 PM

Ok, got it.

This way, it could be a nice feature!

Oct 10, 2012 at 9:54 PM
ahejlsberg wrote:

You can have multiple constructor or method signatures, but only one implementation. For example:

function foo(s: string): string;
function foo(n: number): number;
function foo(x: any): any {
    return x;
}

It is up to the implementation of the function to ensure it actually delivers a result that matches the overload signatures. The compiler does not validate this.

Above works in TypeScript but if foo is a method in a class the multiple signatures are rejected!

Is this supposed to work inside classes?

Developer
Oct 10, 2012 at 11:24 PM

This compiles with no errors:

class Test {
    foo(s: string): string;
    foo(n: number): number;
    foo(x: any): any {
        return x;
    }
}
Oct 11, 2012 at 7:52 AM

Ah, ok, so the implementation overload must have parameters of most generic kind ( any ).

Thanks, i missed this detail.