Use generics to limit event types in on(), once() and un()

This commit is contained in:
Andreas Hocevar
2021-06-20 20:22:53 +02:00
parent cf7213c5cb
commit 018ad97f25
30 changed files with 49 additions and 7 deletions

View File

@@ -5,6 +5,11 @@ import EventTarget from './events/Target.js';
import EventType from './events/EventType.js';
import {listen, listenOnce, unlistenByKey} from './events.js';
/**
* @template ChildTypes
* @typedef {ChildTypes|'change'|'error'|Array<ChildTypes|'change'|'error'>} EventTypes
*/
/**
* @classdesc
* Abstract base class; normally only used for creating subclasses and not
@@ -14,6 +19,7 @@ import {listen, listenOnce, unlistenByKey} from './events.js';
* {@link module:ol/Observable~Observable#changed}.
*
* @fires import("./events/Event.js").default
* @template {string} ChildTypes
* @api
*/
class Observable extends EventTarget {
@@ -48,7 +54,7 @@ class Observable extends EventTarget {
/**
* Listen for a certain type of event.
* @param {string|Array<string>} type The event type or array of event types.
* @param {EventTypes<ChildTypes>} type The event type or array of event types.
* @param {function(?): ?} listener The listener function.
* @return {import("./events.js").EventsKey|Array<import("./events.js").EventsKey>} Unique key for the listener. If
* called with an array of event types as the first argument, the return
@@ -70,7 +76,7 @@ class Observable extends EventTarget {
/**
* Listen once for a certain type of event.
* @param {string|Array<string>} type The event type or array of event types.
* @param {EventTypes<ChildTypes>} type The event type or array of event types.
* @param {function(?): ?} listener The listener function.
* @return {import("./events.js").EventsKey|Array<import("./events.js").EventsKey>} Unique key for the listener. If
* called with an array of event types as the first argument, the return
@@ -94,7 +100,7 @@ class Observable extends EventTarget {
/**
* Unlisten for a certain type of event.
* @param {string|Array<string>} type The event type or array of event types.
* @param {EventTypes<ChildTypes>} type The event type or array of event types.
* @param {function(?): ?} listener The listener function.
* @api
*/