dag*_*oin 8 generics 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]
}
};
Run Code Online (Sandbox Code Playgroud)
这应该是什么意思?
Feature接口需要一个参数:
export interface Feature<T extends GeometryObject> extends GeoJsonObject
{
geometry: T;
properties: any;
id?: string;
}
Run Code Online (Sandbox Code Playgroud)
尝试这个:
const pos = <GeoJSON.Feature<GeoJSON.GeometryObject>>{
"type": "Feature",
"properties":{},
"geometry": {
"type": "Point",
"coordinates": [0, 1]
}
};
Run Code Online (Sandbox Code Playgroud)
也许引入一个辅助类型并在 pos 上设置类型而不是强制转换将帮助您确保已设置所需的“properties”属性:
type GeoGeom = GeoJSON.Feature<GeoJSON.GeometryObject>;
const pos: GeoGeom = {
type: "Feature",
properties: "foo",
geometry: {
type: "Point",
coordinates: [0, 1]
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12945 次 |
| 最近记录: |