Strange Errors with Function Overloading and Recursion

Midway through making this post I found that function overloading isn’t supported. Still found something interesting though, and had a question about a workaround.

I was trying to have a function in a class with different overloads. Here’s the code:

class Foo {
    public static bar(param1: number): string;
    public static bar(param1: string): string;
    public static bar(param1: number | string): string {
        if(typeof param1 === 'number') {
            // If it's a number, call again with param1 as a string.
            return Foo.bar(param1.toString());
        }
        // If it's a string, format it. 
        return "value: " + param1;
    }

This code gives the problem “!proc || !bin.finalPass”.
When changing the function to be non-static:

class Foo {
    public bar(param1: number): string;
    public bar(param1: string): string;
    public bar(param1: number | string): string {
        if(typeof param1 === 'number') {
            // If it's a number, call again with param1 as a string.
            return this.bar(param1.toString());
        }
        // If it's a string, format it. 
        return "value: " + param1;
    }
}

It refused to compile. In the chrome console it gave this error:
‘Uncaught (in promise) Error: TypeError: Cannot read properties of null (reading ‘info’)’
This isn’t the full error, the stacktrace was kind of long.

I know that overloading isn’t supported, but I wanted to point this out since I spent a lot of time figuring out why it wouldn’t compile. Also, I tried not including the two overloads of bar and just having the bar(param1: number | string) and it seemed to work? Is this type of thing fine as a workaround to overloading? Thanks.

1 Like