Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What means "Generic type 'Feature<T>' requires 1 type argument(s)" in Typescript?

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?

like image 337
dagatsoin Avatar asked Apr 22 '16 12:04

dagatsoin


1 Answers

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]
    }
};
like image 74
Corey Alix Avatar answered Nov 12 '22 04:11

Corey Alix