From 81a36a2762da428d3a19ae71810740b2131ed28e Mon Sep 17 00:00:00 2001
From: Ville Immonen <ville.immonen@iki.fi>
Date: Sat, 15 Jan 2022 14:21:55 +0200
Subject: [PATCH] add more accurate typing for YEvent.target

---
 src/structs/ContentType.js | 2 +-
 src/types/AbstractType.js  | 6 +++---
 src/types/YArray.js        | 1 +
 src/types/YMap.js          | 1 +
 src/types/YText.js         | 1 +
 src/types/YXmlEvent.js     | 1 +
 src/utils/Doc.js           | 2 +-
 src/utils/Transaction.js   | 6 +++---
 src/utils/YEvent.js        | 5 +++--
 9 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/src/structs/ContentType.js b/src/structs/ContentType.js
index 30851077..ed83162c 100644
--- a/src/structs/ContentType.js
+++ b/src/structs/ContentType.js
@@ -39,7 +39,7 @@ export const YXmlTextRefID = 6
  */
 export class ContentType {
   /**
-   * @param {AbstractType<YEvent>} type
+   * @param {AbstractType<any>} type
    */
   constructor (type) {
     /**
diff --git a/src/types/AbstractType.js b/src/types/AbstractType.js
index 8a291af1..617ea19b 100644
--- a/src/types/AbstractType.js
+++ b/src/types/AbstractType.js
@@ -278,7 +278,7 @@ export class AbstractType {
     this._eH = createEventHandler()
     /**
      * Deep event handlers
-     * @type {EventHandler<Array<YEvent>,Transaction>}
+     * @type {EventHandler<Array<YEvent<any>>,Transaction>}
      */
     this._dEH = createEventHandler()
     /**
@@ -364,7 +364,7 @@ export class AbstractType {
   /**
    * Observe all events that are created by this type and its children.
    *
-   * @param {function(Array<YEvent>,Transaction):void} f Observer function
+   * @param {function(Array<YEvent<any>>,Transaction):void} f Observer function
    */
   observeDeep (f) {
     addEventHandlerListener(this._dEH, f)
@@ -382,7 +382,7 @@ export class AbstractType {
   /**
    * Unregister an observer function.
    *
-   * @param {function(Array<YEvent>,Transaction):void} f Observer function
+   * @param {function(Array<YEvent<any>>,Transaction):void} f Observer function
    */
   unobserveDeep (f) {
     removeEventHandlerListener(this._dEH, f)
diff --git a/src/types/YArray.js b/src/types/YArray.js
index 63c07a6b..58c7287b 100644
--- a/src/types/YArray.js
+++ b/src/types/YArray.js
@@ -23,6 +23,7 @@ import { typeListSlice } from './AbstractType.js'
 /**
  * Event that describes the changes on a YArray
  * @template T
+ * @extends YEvent<YArray<T>>
  */
 export class YArrayEvent extends YEvent {
   /**
diff --git a/src/types/YMap.js b/src/types/YMap.js
index 612bde0c..a96004e6 100644
--- a/src/types/YMap.js
+++ b/src/types/YMap.js
@@ -21,6 +21,7 @@ import * as iterator from 'lib0/iterator'
 
 /**
  * @template T
+ * @extends YEvent<YMap<T>>
  * Event that describes the changes on a YMap.
  */
 export class YMapEvent extends YEvent {
diff --git a/src/types/YText.js b/src/types/YText.js
index 328795e0..518bfbc2 100644
--- a/src/types/YText.js
+++ b/src/types/YText.js
@@ -502,6 +502,7 @@ const deleteText = (transaction, currPos, length) => {
   */
 
 /**
+ * @extends YEvent<YText>
  * Event that describes the changes on a YText type.
  */
 export class YTextEvent extends YEvent {
diff --git a/src/types/YXmlEvent.js b/src/types/YXmlEvent.js
index 535e55d6..3c2566ed 100644
--- a/src/types/YXmlEvent.js
+++ b/src/types/YXmlEvent.js
@@ -5,6 +5,7 @@ import {
 } from '../internals.js'
 
 /**
+ * @extends YEvent<YXmlElement|YXmlText|YXmlFragment>
  * An Event that describes changes on a YXml Element or Yxml Fragment
  */
 export class YXmlEvent extends YEvent {
diff --git a/src/utils/Doc.js b/src/utils/Doc.js
index 197801fa..ace76392 100644
--- a/src/utils/Doc.js
+++ b/src/utils/Doc.js
@@ -48,7 +48,7 @@ export class Doc extends Observable {
     this.guid = guid
     this.collectionid = collectionid
     /**
-     * @type {Map<string, AbstractType<YEvent>>}
+     * @type {Map<string, AbstractType<YEvent<any>>>}
      */
     this.share = new Map()
     this.store = new StructStore()
diff --git a/src/utils/Transaction.js b/src/utils/Transaction.js
index a9ab6afa..a933056c 100644
--- a/src/utils/Transaction.js
+++ b/src/utils/Transaction.js
@@ -75,13 +75,13 @@ export class Transaction {
      * All types that were directly modified (property added or child
      * inserted/deleted). New types are not included in this Set.
      * Maps from type to parentSubs (`item.parentSub = null` for YArray)
-     * @type {Map<AbstractType<YEvent>,Set<String|null>>}
+     * @type {Map<AbstractType<YEvent<any>>,Set<String|null>>}
      */
     this.changed = new Map()
     /**
      * Stores the events for the types that observe also child elements.
      * It is mainly used by `observeDeep`.
-     * @type {Map<AbstractType<YEvent>,Array<YEvent>>}
+     * @type {Map<AbstractType<YEvent<any>>,Array<YEvent<any>>>}
      */
     this.changedParentTypes = new Map()
     /**
@@ -148,7 +148,7 @@ export const nextID = transaction => {
  * did not change, it was just added and we should not fire events for `type`.
  *
  * @param {Transaction} transaction
- * @param {AbstractType<YEvent>} type
+ * @param {AbstractType<YEvent<any>>} type
  * @param {string|null} parentSub
  */
 export const addChangedTypeToTransaction = (transaction, type, parentSub) => {
diff --git a/src/utils/YEvent.js b/src/utils/YEvent.js
index 3501b587..5d33ef67 100644
--- a/src/utils/YEvent.js
+++ b/src/utils/YEvent.js
@@ -8,17 +8,18 @@ import * as set from 'lib0/set'
 import * as array from 'lib0/array'
 
 /**
+ * @template {AbstractType<any>} T
  * YEvent describes the changes on a YType.
  */
 export class YEvent {
   /**
-   * @param {AbstractType<any>} target The changed type.
+   * @param {T} target The changed type.
    * @param {Transaction} transaction
    */
   constructor (target, transaction) {
     /**
      * The type on which this event was created on.
-     * @type {AbstractType<any>}
+     * @type {T}
      */
     this.target = target
     /**