Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions Overloading in interface and classes - how to?

I have this interface :

interface IPoint {
    getDist(): string;
    getDist(x: number): any;
}

and I need a class to implement it but I can't get the right syntax to implement the getDist() method in the class..

class Point implements IPoint {
    // Constructor
    constructor (public x: number, public y: number) { }

    pointMethod() { }

    getDist() {
        Math.sqrt(this.x * this.x + this.y * this.y);
    }
    // Static member
    static origin = new Point(0, 0);
}

it says:

Class 'Point' declares interface 'IPoint' but does not implement it: Types of property 'getDist' of types 'Point' and 'IPoint' are incompatible:Call signatures of types '() => void' and '{ (): string; (x: number): any; }' are incompatible

What's the proper way to do this?

Thanks

like image 576
Bashar Ali Labadi Avatar asked Nov 04 '12 14:11

Bashar Ali Labadi


People also ask

Can you overload methods in interface?

Yes, you can have overloaded methods (methods with the same name different parameters) in an interface. You can implement this interface and achieve method overloading through its methods.

Can we do function overloading in class?

Function overloading allows you to use the same name for different functions, to perform, either same or different functions in the same class. Function overloading is usually used to enhance the readability of the program.

How do you overload a function?

You overload a function name f by declaring more than one function with the name f in the same scope. The declarations of f must differ from each other by the types and/or the number of arguments in the argument list.

What is Interface overloading?

Overloading. If two methods of an interface (whether both declared in the same interface, or both inherited by an interface, or one declared and one inherited) have the same name but different signatures that are not override-equivalent (§8.4. 2), then the method name is said to be overloaded.


3 Answers

When you declare the function in the class you need to decorate it with the overloads:

getDist(): string;
getDist(x: number): any;
getDist(x?: number): any {
    // your code
 }
like image 95
Fenton Avatar answered Nov 15 '22 06:11

Fenton


This answer describes how to implement method overloading in TypeScript, and it's not pretty:

interface IPoint {
    getDist(): string;
    getDist(x: number): any;
}

class Point implements IPoint {
    // Constructor
    constructor (public x: number, public y: number) { }

    pointMethod() { }

    getDist(x?: number) {
         if (x && typeof x == "number") {
             return 'foo';
         } else {
             return 'bar';
         }
    }
}

N.B. with the particular combination of declared return types in the interface, you are limited to returning strings from getDist.

like image 22
Matt Ball Avatar answered Nov 15 '22 07:11

Matt Ball


also you can use the default value

interface Foo{
    next()
    next(steps: number)
    prev()
    prev(steps: number)
}

next(steps: number = 1) {
    // ...
}

prev(steps: number = 1) {
    // ...
}
like image 43
Pax Exterminatus Avatar answered Nov 15 '22 08:11

Pax Exterminatus