I have a Leaflet map that contains some GeoJSON layers (and other layers besides). I want to iterate over the layers to find the GeoJSON layer with a property matching some target value. The problem I have is getting the type annotations right, such that I don't get warnings from the TypeScript compiler.
The core issue is:
const map:L.map = ...
map?.eachLayer(layer => {
// @ts-expect-error GeoJSON layers do have a feature property
const feature = layer.feature
if (feature) {
const layerId = feature.properties['someIdProperty']
if (layerId === targetId) {
// @ts-expect-error We know that this layer has bounds
map?.fitBounds(layer.getBounds())
}
}
})
The code does run correctly, but I can't find a way to type the layer
variable to avoid getting compiler warnings. Hence resorting to the ugly @ts-expect-error
hack.