Error: "Overload signature is not compatible with function definition" when not using "any" overload

Topics: General
Nov 4, 2012 at 1:52 AM

Using this code

function myFunction(a:string);
function myFunction(a:Array) {
    // ...
}

produces the error: "Overload signature is not compatible with function definition".

On the other hand adding an "any" overload doesn't produce the error:

function myFunction2(a:string);
function myFunction2(a:Array);
function myFunction2(a:any) {
    // ...
}

I actually don't quite understand what the error means. I tried calling myFunction2 with a number and it seems to only expect strings or Arrays - the result I would expect from the version without "any". Does the "any" just always have to be there when overloading??

Thanks for the answer ...

Coordinator
Nov 5, 2012 at 4:05 PM

For a function, there's the type side and the implementation side.  When you create an overload signature, like you have, you're adding more information to the type side.  This also has the (perhaps strange) side effect of moving the type side away from the function.

Roughly, then:

//type side
function myFunction2(a:string);
function myFunction2(a:Array);

//implementation side
function myFunction2(a:any) {

    // ...
}