typescriptgisopenlayersvector-tiles

custom event listener in OpenLayers


I created a class that extend Interaction in openlayers. This class allows to select vector tiles features.

/**
 * Vector Tile selector class.
 */
export class VectorTileSelect extends Interaction {
  constructor() {
    super({
      handleEvent: (evt: MapBrowserEvent<UIEvent>): boolean => {
        return this.selectFeaturesAtPixel(evt);
      },
    });
  }

  /**
   * Select vector features at a given pixel and fires
   * a vector tile select event on selection (select:vectortile)
   * @param e - Map browser event
   * @returns true when the selection is made
   */
  private selectFeaturesAtPixel(e: MapBrowserEvent<UIEvent>): boolean {
    if (e.type === 'click') {
      const features = this.getMap()?.getFeaturesAtPixel(e.pixel, {
        layerFilter: (layer) => layer instanceof VectorTileLayer,
      });

      this.dispatchEvent(new VectorTileSelectEvent(features, e));
    }
    return true;
  }
}

When a selection is made, the following custom event is fired:

/**
 * Vector tile selection event
 */
export class VectorTileSelectEvent extends Event {
  private selected: FeatureLike[] | undefined;
  private mapBrowserEvent: MapBrowserEvent<UIEvent>;

  constructor(
    selected: FeatureLike[] | undefined,
    mapBrowserEvent: MapBrowserEvent<UIEvent>
  ) {
    super('select:vectortile');
    this.selected = selected;
    this.mapBrowserEvent = mapBrowserEvent;
  }
}

But when i try to listen to this event like that :

  const yolo = new VectorTileSelect();
  map.addInteraction(yolo);

  yolo.on('select:vectortile', (e) => {
    console.log(e);
  });

TS throw me the following error :

No overload matches this call.
  Overload 1 of 3, '(type: EventTypes, listener: (event: BaseEvent) => unknown): EventsKey', gave the following error.
  Overload 2 of 3, '(type: "propertychange" | "change:active", listener: (event: ObjectEvent) => unknown): EventsKey', gave the following error.
  Overload 3 of 3, '(type: (EventTypes | "propertychange" | "change:active")[], listener: (event: BaseEvent | Event) => unknown): EventsKey[]', gave the following error.ts(2769).

How can i fix that ?


Solution

  • To answer my own question, OpenLayers doesn't allows dispatching custom events. cf. https://github.com/openlayers/openlayers/issues/14667