generics - typescript 中的 "Generic type ' Feature<T >' requires 1 type argument(s)"是什么意思?

标签 generics typescript geojson

我尝试在 typescript 中使用 GeoJson,但编译器针对这两个变量抛出错误: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]
    }
  };

这是什么意思?

最佳答案

Feature接口(interface)需要一个参数:

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

关于generics - typescript 中的 "Generic type ' Feature<T >' requires 1 type argument(s)"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36794496/

相关文章:

Java 通用问题 : Any better way?

java - 以下泛型声明在 Java 中有何不同?

typescript - Angular 2 - 为什么我需要 zone.run()?

typescript - 不要使用对象作为类型

javascript - 在 Dart 中使用 geoJson Leaflet

javascript - 关于如何快速从 geojson 文件中删除多个功能的任何建议?

javascript - 标尺控制错误: Uncaught SyntaxError: Unexpected identifier

java - 如何将类型安全对象强制转换为 JComboBox<String>?

c# - 是否可能有一个需要开放通用接口(interface)的通用约束?

angular - ngx-bootstrap 弹出窗口在外部点击时关闭