import Group from "ol/layer/Group"; import type { Collection } from "ol"; import type BaseLayer from "ol/layer/Base"; const getAllLayers = ( layersOrLayerGroup: BaseLayer[] | Collection | Group, ) => { const allLayers = []; function addLayersFrom(layers: BaseLayer[] | Collection) { layers.forEach(function (layer) { if (layer instanceof Group) { addLayersFrom(layer.getLayers()); } else { allLayers.push(layer); } }); } addLayersFrom( (layersOrLayerGroup as Group)?.getLayers?.() || (layersOrLayerGroup as BaseLayer[] | Collection), ); return allLayers; }; export default getAllLayers;