mapbox / cheap-ruler

Fast approximations for common geodesic measurements 🌐
https://www.mapbox.com/blog/cheap-ruler/
ISC License
417 stars 32 forks source link

Added Typescript definition to library #23

Closed DenisCarriere closed 7 years ago

DenisCarriere commented 7 years ago

Happy new year! 🎉

Included the Typescript definition to this repo.

@mourner

FYI: Purposely didn't update @turf/turf package since it breaks the tests.

mourner commented 7 years ago

Since I'm not a TypeScript user and won't be maintaining the definition, could this stay at DefinitelyTyped? I know it's more convenient to have it here, but this really increases the maintenance burden for me.

DenisCarriere commented 7 years ago

@mourner It's up to you, I personally will make sure to maintain this. It's not very often that the overall cheap-ruler functionality change or break backwards compatibility.

How about a compromise... I strip off all the comments and leave only the index.d.ts core methods?

DenisCarriere commented 7 years ago

@mourner I've converted the definition file to be "low maintenance", since all your documentation & JSDocs is already inside your .js source code.

It's 35 lines of code now, very easy to manage.

Commit https://github.com/mapbox/cheap-ruler/pull/23/commits/792857da683a28fdfc637e63c8c0f24dd15c7e72

index.d.ts

declare function cheapRuler(lat: number, units?: string): cheapRuler.CheapRuler;
declare namespace cheapRuler {
    type BBox = [number, number, number, number]
    type Point = [number, number]
    type Line = Point[]
    type Points = Point[]
    type Polygon = Point[][]

    class CheapRuler {
        distance(a: cheapRuler.Point, b: Point): number;
        bearing(a: Point, b: Point): number;
        destination(p: Point, dist: number, bearing: number): Point;
        lineDistance(points: Points): number;
        area(polygon: Polygon): number;
        along(line: Line, dist: number): Point;
        pointOnLine(line: Line, p: Point): {point: Point, index: number, t: number};
        lineSlice(start: Point, stop: Point, line: Line): Line;
        lineSliceAlong(start: number, stop: number, line: Line): Line;
        bufferPoint(p: Point, buffer: number): BBox;
        bufferBBox(bbox: BBox, buffer: number): BBox;
        insideBBox(p: Point, bbox: BBox): boolean;
    }
    const units: {
        kilometers: number;
        miles: number;
        nauticalmiles: number;
        meters: number;
        metres: number;
        yards: number;
        feet: number;
        inches: number;
    }
    function fromTile(y: number, z: number, units?: string): CheapRuler;
}
export = cheapRuler;
DenisCarriere commented 7 years ago

@mourner Added Typescript tests to your package.json.

Commit https://github.com/mapbox/cheap-ruler/pull/23/commits/3e557b1418640fe39fd9b65b9f2c8bb792b920ee

For example, if I would change the following

type Point = [number, number]
type Point = string

Run the tests, it would cause an error like this.

$ tsc test/types.ts
test/types.ts(7,7): error TS2322: Type 'number[]' is not assignable to type 'string'
.
test/types.ts(8,7): error TS2322: Type 'number[]' is not assignable to type 'string'
.
test/types.ts(9,7): error TS2322: Type 'number[][][]' is not assignable to type 'str
ing[][]'.
  Type 'number[][]' is not assignable to type 'string[]'.
    Type 'number[]' is not assignable to type 'string'.
test/types.ts(13,7): error TS2322: Type 'number[][]' is not assignable to type 'stri
ng[]'.
  Type 'number[]' is not assignable to type 'string'.
test/types.ts(34,18): error TS2345: Argument of type 'number[]' is not assignable to
 parameter of type 'string'.
DenisCarriere commented 7 years ago

One of the main reasons why I'm including this file inside your repo is because I'm tired of importing tons of @types module, there's already enough imported JS packages as it is. My project's devDependencies is growing every week.

Also, maybe one day, Flow & Typescript will be compatible with each to other.

mourner commented 7 years ago

Thanks! The PR looks great now.

DenisCarriere commented 7 years ago

@mourner thanks! I hope you don't mind me doing this for a few other repos of yours, I use all your libraries, they are great!