Skip to content

Should getXmlElement accept a type argument? #748

Description

@krassowski

Checklist

[x] Are you reporting a bug? Use github issues for bug reports and feature requests. For general questions, please use https://discuss.yjs.dev/
[x] Try to report your issue in the correct repository. Yjs consists of many modules. When in doubt, report it to https://github.com/yjs/yjs/issues/

Is your feature request related to a problem? Please describe.

I wrote code like:

const el = doc.getXmlElement('key')
const attrs = el.getAttributes()

I incorrectly assumed that attrs have to be strings because doc.getXmlElement casts to YXmlElement<{[key:string]:string}> here:

yjs/src/utils/Doc.js

Lines 302 to 304 in 19b5134

getXmlElement (name = '') {
return /** @type {YXmlElement<{[key:string]:string}>} */ (this.get(name, YXmlElement))
}

but in fact XMLElement can have attributes of any type:

* @template {{ [key: string]: any }} [Attrs={ [key: string]: string }]
* @template {any} [Children=any]
* @extends YXmlFragment<Children,Attrs>
*/
export class YXmlElement extends YXmlFragment {
constructor (nodeName = 'UNDEFINED') {
super()
this.nodeName = nodeName
/**
* @type {Map<string, any>|null}
*/
this._prelimAttrs = new Map()
}

or of type ValueTypes in v13:

* * An YXmlElement has attributes (key value pairs)
* * An YXmlElement has childElements that must inherit from YXmlElement
*
* @template {{ [key: string]: ValueTypes }} [KV={ [key: string]: string }]
*/
export class YXmlElement extends YXmlFragment {

I then tried to correct it by writing

const el = doc.getXmlElement<[key:string]: any}>('key')

But typechecker rejected it because getXmlElement does not accept type arguments

Describe the solution you'd like

  1. Make the default return type annotation use any to protect users
  2. Add both type arguments to doc.getXmlElement

Describe alternatives you've considered
None

Additional context
Add any other context or screenshots about the feature request here.

  • I'm a sponsor 💖
  • This feature is critical for my project.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions