I've been experimenting with overloads (originally trying to implement this basic proxy example in typescript without foul tricks like disabling type checking on a line, if any of you have solved that feel free to spoiler me, I have already learned much and its getting annoying that I cant seem to solve it).
I've been running into an problem while experimenting. Here's my code:
// "Overload signature is not compatible with function implementation." on first (in code) declaration
function strOrObj (samePar:object, obj:object):object|undefined;
function strOrObj (samePar:object, num:number):number|undefined;
function strOrObj (samePar:object, num?:number, obj?:object):object|number|undefined{
console.log("obj="+obj, "num="+num);
return obj? obj: num;
}
I had to add the undefined on the implementation because the compiler, looking at the implementation, thought it might be needed. It didnt ask for me to add this to the overrides, but I did it anyways to experiment. But when I add type declarations in the implementation I get the error Overload signature is not compatible with function implementation..
The only way I get this to compile is to declare any parameter in the implementation declaration as any, but that way it'll just stuff every given type into the first optional parameter (num in this case).
So the queston is, what is wrong here? Is it even possible to have multiple optional parameters if youre not sure the first one will be used or is that maybe the problem (which could be a thing because of the JS implementation)?
Sorry if this is a duplicate, the only thing I could find that seemed to apply was an issue on Github, but the fixes seem to have been merged into the release code.