Skip to content

Commit

Permalink
better typings for ydoc.get
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Feb 9, 2024
1 parent 16d9638 commit e1bce03
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/types/YXmlElement.js
Expand Up @@ -20,7 +20,7 @@ import {

/**
* An YXmlElement imitates the behavior of a
* {@link https://developer.mozilla.org/en-US/docs/Web/API/Element|Dom Element}.
* https://developer.mozilla.org/en-US/docs/Web/API/Element|Dom Element
*
* * An YXmlElement has attributes (key value pairs)
* * An YXmlElement has childElements that must inherit from YXmlElement
Expand Down
22 changes: 9 additions & 13 deletions src/utils/Doc.js
Expand Up @@ -181,6 +181,7 @@ export class Doc extends Observable {
* Define all types right after the Yjs instance is created and store them in a separate object.
* Also use the typed methods `getText(name)`, `getArray(name)`, ..
*
* @template {typeof AbstractType<any>} Type
* @example
* const y = new Y(..)
* const appState = {
Expand All @@ -189,12 +190,12 @@ export class Doc extends Observable {
* }
*
* @param {string} name
* @param {Function} TypeConstructor The constructor of the type definition. E.g. Y.Text, Y.Array, Y.Map, ...
* @return {AbstractType<any>} The created type. Constructed with TypeConstructor
* @param {Type} TypeConstructor The constructor of the type definition. E.g. Y.Text, Y.Array, Y.Map, ...
* @return {InstanceType<Type>} The created type. Constructed with TypeConstructor
*
* @public
*/
get (name, TypeConstructor = AbstractType) {
get (name, TypeConstructor = /** @type {any} */ (AbstractType)) {
const type = map.setIfUndefined(this.share, name, () => {
// @ts-ignore
const t = new TypeConstructor()
Expand All @@ -220,12 +221,12 @@ export class Doc extends Observable {
t._length = type._length
this.share.set(name, t)
t._integrate(this, null)
return t
return /** @type {InstanceType<Type>} */ (t)
} else {
throw new Error(`Type with the name ${name} has already been defined with a different constructor`)
}
}
return type
return /** @type {InstanceType<Type>} */ (type)
}

/**
Expand All @@ -236,8 +237,7 @@ export class Doc extends Observable {
* @public
*/
getArray (name = '') {
// @ts-ignore
return this.get(name, YArray)
return /** @type {YArray<T>} */ (this.get(name, YArray))
}

/**
Expand All @@ -247,7 +247,6 @@ export class Doc extends Observable {
* @public
*/
getText (name = '') {
// @ts-ignore
return this.get(name, YText)
}

Expand All @@ -259,8 +258,7 @@ export class Doc extends Observable {
* @public
*/
getMap (name = '') {
// @ts-ignore
return this.get(name, YMap)
return /** @type {YMap<T>} */ (this.get(name, YMap))
}

/**
Expand All @@ -270,8 +268,7 @@ export class Doc extends Observable {
* @public
*/
getXmlElement (name = '') {
// @ts-ignore
return this.get(name, YXmlElement)
return /** @type {YXmlElement<{[key:string]:string}>} */ (this.get(name, YXmlElement))
}

/**
Expand All @@ -281,7 +278,6 @@ export class Doc extends Observable {
* @public
*/
getXmlFragment (name = '') {
// @ts-ignore
return this.get(name, YXmlFragment)
}

Expand Down
2 changes: 0 additions & 2 deletions tests/y-xml.tests.js
Expand Up @@ -189,7 +189,6 @@ export const testClone = _tc => {
const third = new Y.XmlElement('p')
yxml.push([first, second, third])
t.compareArrays(yxml.toArray(), [first, second, third])

const cloneYxml = yxml.clone()
ydoc.getArray('copyarr').insert(0, [cloneYxml])
t.assert(cloneYxml.length === 3)
Expand Down Expand Up @@ -217,7 +216,6 @@ export const testFormattingBug = _tc => {
export const testElement = _tc => {
const ydoc = new Y.Doc()
const yxmlel = ydoc.getXmlElement()

const text1 = new Y.XmlText('text1')
const text2 = new Y.XmlText('text2')
yxmlel.insert(0, [text1, text2])
Expand Down

0 comments on commit e1bce03

Please sign in to comment.