typescripttypesleafletgisgeojson

How to parse GeoJSON in TypeScript with the geojson module and the @types/geojson type?


My TypeScript application is reporting that property parse does not exist on ./path/to/node_modules/@types/geojson/index. Which is true when inspecting the interfaces for the geojson type definition.

The geojson module does however offer a parse method.

How can I access the parse method?

I tried removing the @types/geojson module which seems to make the method accessible. However, despite having removed the type definition the same exception is thrown or, in another scenario I've not been able to replicate, VS Code throws an error that a type definition is recommended and won't compile without it.

I added the // @ts-ignore decorator to disable the error but I'd prefer a cleaner fix.


Solution

  • Unfortunately you bumped on a rare case where a DefinitelyTyped @types/xxx package does NOT describe the same named xxx package on npm...

    This package contains type definitions for geojson (https://geojson.org/).

    // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/geojson/package.json#L5
    {
        "name": "@types/geojson",
        "nonNpm": true,
        "nonNpmDescription": "geojson"
    }
    

    As mentioned in the README of that library:

    For Typescript environments, you can use GeoJSON.ts, a Typescript-native library. @EugeneYWang/GeoJSON.ts

    ...and the full explanation on the latter:

    The reason to create this repo

    Usually, an existed NPM package can just be used in TypeScript if a type definition file with the same name of that package is also installed. So why should I refactor that library into this typescript one? The reason is that the type definition named as GeoJSON has been occupied as the type definition of GeoJSON standard specification. Therefore, no one could contribute a type definition in TypeDefinition for @caseycesari/GeoJSON.js.