4

Here's an example:

class A {
  func(): void {}
}

class B extends A {
  func(a: number, b: string): void {}
}

Class B gives an error saying func() is implemented incorrectly.

Ultimately, I'm trying to make this work:

var b: B;
b.func(0, '');     // func is overloaded by B
b.func();          // func is inherited from A

Is this currently possible in Typescript?

UPDATE: Fixed the code, accidentally used function properties instead of methods.

4 Answers 4

3

When using arrow functions you don't really get class methods but members of type/value of a function.
The difference being that while methods are added to the prototype, the arrow functions are added to the instance in the constrctor:

class MyClass {
    method() {}
    funct = () => {}
}

Compiles to:

var MyClass = (function () {
    function MyClass() {
        this.func = function () { };
    }
    MyClass.prototype.method = function () { };
    return MyClass;
}());

That's fine of course, if that's what you want.
The main problem with that is that overloading and calling the parent method aren't as simple.

In your case, with overloading:

class A {
    func(): void {}
}

class B extends A {
    func(): void; // parent signature
    func(a: number, b: string): void; // new signature
    func(): void {
        if (arguments.length === 0) {
            super.func();
        } else {
            // actual implementation
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Since TypeScript is compiled to Javascript.

JavaScript doesn't know how many parameters passed to the function, and type of each parameter to properly decide which function to call automatically. So you have to use different function name for that. Or you have to check for the actual type of parameters when run time:

class B extends A {
  func() => void;
  func(a: number, b: string) => void;
  func(a: any, b: any) {
    if (a == undefined) {
      super.func()
    } else if (typeof(a) == "number" && typeof(b) == "String") {
      /// implement func(a: b) here
    }
  }

}

Comments

1

There isn't a way to automatically get your base class's overload signatures. You'd need to write something like this:

class A {
  func: () => void;
}

class B extends A {
  func: {
      (): void;
      (a: number, b: string): void;
  }
}

As noted by the other answer, you're on your own for correctly implementing func to handle the 0-arg case.

1 Comment

I accidentally used function properties instead of methods. Is there a way to make the methods work? (updated in the question above)
0

According to TypeScript documentation for classes you need to add "?" after parameter name in the overridden / overloaded method (also, add override keyword before method name - I get a compilation / transpilation error when I don't):

class A {
  func(): void {}
}

class B extends A {
  override func(a?: number, b?: string): void {}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.