【问题标题】:What means "Generic type 'Feature<T>' requires 1 type argument(s)" in Typescript?Typescript中的“通用类型'Feature<T>'需要1个类型参数”是什么意思?
【发布时间】:2016-08-16 02:58:57
【问题描述】:

我尝试在 typescript 中使用 GeoJson,但编译器会为这两个变量抛出错误:Generic type 'Feature&lt;T&gt;' 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]
    }
  };

这是什么意思?

【问题讨论】:

  • 试试 >

标签: generics typescript geojson


【解决方案1】:

Feature接口需要一个参数:

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

试试这个:

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

也许引入一个辅助类型并在 pos 上设置类型而不是强制转换将帮助您确保您已设置所需的“属性”属性:

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

【讨论】:

  • 抱歉回复晚了,但确实有效。您能否在“试试这个”之后的代码块中添加“属性”:{}。这种方式无效,缺少“属性”属性。谢谢你!
猜你喜欢
  • 2017-06-05
  • 2017-10-23
  • 2018-12-08
  • 2017-01-09
  • 1970-01-01
  • 2017-02-08
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多