用示例解释起来比较简单。看一下这段代码:
const network:any = this.props.network;
this.map.addLayer({
id: "route",
type: "line",
source: {
type: "geojson",
data: network
},
layout: {
"line-join": "round",
"line-cap": "round"
},
paint: {
"line-color": "red",
"line-width": 2
}
});
this.props.network
设置在其他地方,其代码类似于:
const response = await fetch("http://localhost:5000/network");
this.props.network = await response.json();
此代码可扩展使用。但是我想为this.props.network
正确设置te type,以删除any
解决方法。 network
是有效的geojson对象,VS代码向我显示此提示:
所以我猜猜正确的类型是:FeatureCollection<Geometry, GeoJsonProperties>
但是我不知道这些类型在哪里定义以及我必须导入什么;
您要查找的导入位于geojson
包中。因此,导入看起来像:
import { FeatureCollection, Geometry, GeoJsonProperties } from "geojson";
[如果查看mapbox-gl
的类型定义文件,可以看出geojson
类型在这里被引用:Mapbox-GL .d.ts file