什么意思是“通用类型”特征< T>“需要1个类型参数“在打字稿? [英] What means "Generic type 'Feature<T>' requires 1 type argument(s)" in Typescript?

查看:293
本文介绍了什么意思是“通用类型”特征< T>“需要1个类型参数“在打字稿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在typescript中使用GeoJson,但编译器为这两个变量抛出错误: Generic type'Feature< T>'需要1个类型参数

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?

推荐答案

Feature接口需要一个参数:

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

也许引入一个辅助类型并在pos上设置类型而不是cast会帮助您确保'设置了所需的'properties'属性:

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

这篇关于什么意思是“通用类型”特征&lt; T&gt;“需要1个类型参数“在打字稿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆