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

本小妞迷上赌 提交于 2019-12-23 07:48:58

问题


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?


回答1:


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]
    }
};


来源:https://stackoverflow.com/questions/36794496/what-means-generic-type-featuret-requires-1-type-arguments-in-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!