【发布时间】:2022-01-11 08:15:03
【问题描述】:
我正在尝试使用 typescript 将 geoJSON 添加到 react-map-gl 地图中
这是我的代码
<Source id="my-data" type="geojson" data={data}>
<Layer {...layerStyles}/>
</Source>
data 变量是一个解析的 JSON 文件,所以它不是一个对象
我得到的错误是
Overload 1 of 2, '(props: SourceProps | Readonly<SourceProps>): Source', gave the following error.
Type 'object' is not assignable to type 'string | Feature<Geometry, GeoJsonProperties> | FeatureCollection<Geometry, GeoJsonProperties> | undefined'.
Type '{}' is missing the following properties from type 'FeatureCollection<Geometry, GeoJsonProperties>': type, features
Overload 2 of 2, '(props: SourceProps, context: any): Source', gave the following error.
Type 'object' is not assignable to type 'string | Feature<Geometry, GeoJsonProperties> | FeatureCollection<Geometry, GeoJsonProperties> | undefined'. TS2769
123 | onViewportChange={(nextView:typeof viewport) => setViewport(nextView)}>
124 | {/* GeoJSON */}
> 125 | <Source id="my-data" type="geojson" data={data}>
| ^
126 | <Layer {...layerStyles}/>
127 | </Source>
128 | {markers}
Source 组件中的数据 props 的类型应该是一个对象,如您从文档 https://visgl.github.io/react-map-gl/docs/api-reference/source 中看到的那样
如果您需要更多信息,请询问
【问题讨论】:
-
在这种情况下
data是什么? -
这是一个 JSON 文件,包含我导入的所有 geoJSON 然后解析成一个对象,它是一个包含 geoJSON 的对象吗
-
当它到达此代码时,您是否验证了形状是正确的?显然
Source组件不同意并认为data缺少名为type和features的属性。 -
是的,数据既有类型又有特性,我可以对它们进行控制台记录。
-
你能在这里分享吗?
标签: reactjs typescript typeerror mapbox react-map-gl