Mapbox图层的正确类型(打字稿)在哪里?

问题描述 投票:0回答:1

用示例解释起来比较简单。看一下这段代码:

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代码向我显示此提示:

enter image description here

所以我猜猜正确的类型是:FeatureCollection<Geometry, GeoJsonProperties>

但是我不知道这些类型在哪里定义以及我必须导入什么;

reactjs typescript mapbox
1个回答
0
投票

您要查找的导入位于geojson包中。因此,导入看起来像:

import { FeatureCollection, Geometry, GeoJsonProperties } from "geojson";

[如果查看mapbox-gl的类型定义文件,可以看出geojson类型在这里被引用:Mapbox-GL .d.ts file

© www.soinside.com 2019 - 2024. All rights reserved.