Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I would like to add a typescript definition #58

Open
basile-savouret opened this issue Dec 16, 2022 · 1 comment
Open

I would like to add a typescript definition #58

basile-savouret opened this issue Dec 16, 2022 · 1 comment

Comments

@basile-savouret
Copy link

basile-savouret commented Dec 16, 2022

Hi @caseycesari, i love your library and all the work that has been done here !

But it is missing type definitions and the fact that @types/geojson isn't providing it is really confusing at the start of using the library.
I saw that someone did a typescript version on a fork of the library here but overall it's just a duplication of the current js with addition of bad types on top of it.

I added a custom type file on my side that types the lib with modern and precise typescript:

import {FeatureCollection, Feature, Geometry} from '@types/geojson'

export interface InvalidGeometryError extends Error {
    item: any;
    params: any;
}

export type Geoms = 'Point' | 'MultiPoint' | 'LineString' | 'MultiLineString' | 'Polygon' | 'MultiPolygon' | 'GeoJSON'

export interface GeomsParams extends Partial<Record<Geoms, string>> {
    Point?: string | string[]
}

export type Data = { [key: string]: any } | any[]

export interface Params extends GeomsParams {
    doThrows?: {
        invalidGeometry: boolean;
    };
    removeInvalidGeometries?: boolean;
    extraGlobal?: { [key: string]: any };
    extra?: { [key: string]: any };
    crs?: any;
    bbox?: any[];
    include?: string[];
    exclude?: string[];
    isPostgres?: boolean;
    GeoJSON?: string
}

export interface GEOJSON {
    version: string;
    defaults: Params;
    errors: {
        InvalidGeometryError: InvalidGeometryError;
    };
    isGeometryValid(geometry: Geometry): boolean;
    parse<D extends Data = any, G extends Geometry | null = Geometry, P = GeoJsonProperties>(data: D, params?: Params, callback?: (geojson: D extends any [] ? FeatureCollection<G, P> : Feature<G, P>) => void): D extends any [] ? FeatureCollection<G, P> : Feature<G, P>;
}

const GeoJSON: GEOJSON;
export default GeoJSON;
export * from "@types/geojson"

It uses @types/geojson and extend it to provide the types of your library.
It works perfectly, i have tested it with all the examples provided in the readme.

It won't change anything for the js part i would just add a geojson.d.ts file and declare it in the package.json.
I just need your feedback on the typescript if you have one and your approval for the pull request.

@basile-savouret
Copy link
Author

basile-savouret commented Jan 20, 2023

As this project seems to be abandoned, i will provide more details for those who wants to use my types for the library.

You can declare the types for the module geojson in your global typing.d.ts file at the root of your project, just like that:

declare module 'geojson' {
...All the types i wrote above
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant