gpt4 book ai didi

reactjs - 用 typescript react map gl

转载 作者:行者123 更新时间:2023-12-02 02:00:10 30 4
gpt4 key购买 nike

你好,我是 React 的新手,但我选择使用 typescript (我也是新手)而不是原始 JS。话虽如此,我在向 react-map-gl 元素添加图层时遇到了麻烦。这是我得到的错误代码

Type '{ id: string; type: string; paint: { 'sky-type': string; 'sky-atmosphere-sun': number[]; 'sky-atmosphere-sun-intensity': number; }; }' is not assignable to type 'LayerProps'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"symbol" | "sky" | "circle" | "line" | "fill" | "fill-extrusion" | "raster" | "background" | "heatmap" | "hillshade"'.ts(2322)

它看起来好像说它想要一个枚举?以下是代码片段:

const skyLayer = {
id: 'sky',
type: 'sky',
paint: {
'sky-type': 'atmosphere',
'sky-atmosphere-sun': [0.0, 0.0],
'sky-atmosphere-sun-intensity': 15
}
};

还有这个给我红色波浪形的(在层下):

<Layer {...skyLayer} />

以及我尝试遵循的示例代码: https://github.com/visgl/react-map-gl/blob/6.1-release/examples/terrain/src/app.js

编辑 1:我按照代码中的建议进行了更改,但似乎无法解决问题。这是我收到的错误:

Type '{ id: string; type: "sky"; paint: { "sky-type": string; "sky-atmosphere-sun": number[]; "sky-atmosphere-sun-intensity": number; }; }' is not assignable to type 'LayerProps'.
Types of property 'paint' are incompatible.
Type '{ "sky-type": string; "sky-atmosphere-sun": number[]; "sky-atmosphere-sun-intensity": number; }' is not assignable to type 'BackgroundPaint | FillPaint | FillExtrusionPaint | LinePaint | SymbolPaint | RasterPaint | CirclePaint | HeatmapPaint | HillshadePaint'.
Type '{ "sky-type": string; "sky-atmosphere-sun": number[]; "sky-atmosphere-sun-intensity": number; }' has no properties in common with type 'HillshadePaint'.ts(2322)

最佳答案

在声明skyLayer 时,您应该为type 声明数据类型。因为默认的 type 是字符串,不能传入 Layer。您可以使用 as 来做到这一点:

  const skyLayer = {
id: 'sky',
type: 'sky' as 'sky',
paint: {
'sky-type': 'atmosphere',
'sky-atmosphere-sun': [0.0, 0.0],
'sky-atmosphere-sun-intensity': 15
}
};

关于reactjs - 用 typescript react map gl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69088645/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com