我尝试在 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/