Functions Overloading in interface and classes - how to?
class, interface, overloading, typescript
Solution
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
}
Problem
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