better typings for ydoc.get
This commit is contained in:
parent
16d9638bc8
commit
e1bce03ed8
@ -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
|
||||
|
@ -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 = {
|
||||
@ -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()
|
||||
@ -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)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -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))
|
||||
}
|
||||
|
||||
/**
|
||||
@ -247,7 +247,6 @@ export class Doc extends Observable {
|
||||
* @public
|
||||
*/
|
||||
getText (name = '') {
|
||||
// @ts-ignore
|
||||
return this.get(name, YText)
|
||||
}
|
||||
|
||||
@ -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))
|
||||
}
|
||||
|
||||
/**
|
||||
@ -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))
|
||||
}
|
||||
|
||||
/**
|
||||
@ -281,7 +278,6 @@ export class Doc extends Observable {
|
||||
* @public
|
||||
*/
|
||||
getXmlFragment (name = '') {
|
||||
// @ts-ignore
|
||||
return this.get(name, YXmlFragment)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
@ -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])
|
||||
|
Loading…
x
Reference in New Issue
Block a user