alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
535 stars 56 forks source link

Fix: TS definition for "translate" method of Point class #150

Closed aiiak closed 11 months ago

aiiak commented 11 months ago

Fix typescript definitions for 'translate' metthod of Point class.

alexbol99 commented 11 months ago

Hi, @aiiak , Typescript definitions for translate method moved to base class Shape

aiiak commented 11 months ago

Hi, @alexbol99

Definition for translate method in base class stricktly define type of result object as Shape

class Shape {
        translate(vec: Vector): Shape;
...
}

And we lose type after applying translate, for example

const p = new Flatten.Point(0,1).translate(1, 1); 

Variable p will have type Shape, but shoul be Point

There is already some overrides for translate definition in index.d.ts , like for Polygon , Multiline and Matrix , f.e.

class Polygon {
    ...
    translate(vec: Vector): Polygon;
    ...
}

So, this PR is make same override for Point class.

But if every method, defined in Shape class should return same object, that call method, following change in Shape definition should be more correct.

    class Shape {
        translate(vec: Vector): typeof this;
        translate(x:number, y:number): typeof this;
        rotate(angle: number, center?: Point): typeof this;
        scale(scaleX: number, scaleY: number) : typeof this;
        transform(matrix: Matrix): typeof this;
    }