16

I try to use GeoJson in typescript but the compiler throws error for this two variables: Generic type 'Feature<T>' requires 1 type argument(s)

  const pos = <GeoJSON.Feature>{
    "type": "Feature",
    "geometry": {
      "type": "Point",
      "coordinates": [0, 1]
    }
  };

  const oldPos = <GeoJSON.Feature>{
    "type": "Feature",
    "geometry": {
      "type": "Point",
      "coordinates": [2, 4]
    }
  };

What is this supposed to mean?

dagatsoin
  • 2,626
  • 6
  • 25
  • 54

1 Answers1

4

The Feature interface requires a parameter:

export interface Feature<T extends GeometryObject> extends GeoJsonObject
{
    geometry: T;
    properties: any;
    id?: string;
}

Try this:

  const pos = <GeoJSON.Feature<GeoJSON.GeometryObject>>{
    "type": "Feature",
    "properties":{},
    "geometry": {
      "type": "Point",
      "coordinates": [0, 1]
    }
  };

And maybe introduce a helper type and set the type on pos instead of casting will help you ensure you've set the required 'properties' attribute:

type GeoGeom = GeoJSON.Feature<GeoJSON.GeometryObject>;
const pos: GeoGeom = {
    type: "Feature",
    properties: "foo",
    geometry: {
        type: "Point",
        coordinates: [0, 1]
    }
};
Corey Alix
  • 2,694
  • 2
  • 27
  • 38
  • sorry for the late reply, but yes indeed it work. Could you plz add "properties":{} in the code block after "Try this". It is not valid this way, missing "properties" property. Thx you ! – dagatsoin Apr 29 '16 at 15:39