JSDOC不拾取数据子属性类型覆盖

发布于 2025-02-03 08:14:45 字数 1004 浏览 1 评论 0原文

我注意到与子班延长的父母课程一起工作时有一个问题。 这两个类都有一个“数据”属性,在父类中,它是一种通用的“对象”类型,在子类中,它是类型为“ dogdata”的类型。

父母的构造函数正在设置“ this.data = data”,该类型应覆盖孩子的属性类型是什么,但这并没有发生。

请在下面找到一个小的可重复的示例:

我希望我错过了一些东西,如果我是,请不要犹豫地指出。

提前致谢

 * The animal base class
 * @class
 */
class Animal {
    /**
     * @param {number} id The ID of the animal
     * @param {Object} data The data of the animal
     */
    constructor (id, data) {
        this.id = id
        this.data = data
    }
}

/**
 * The Dog class
 * @class
 * @extends Animal
 */
class Dog extends Animal {

    /**
     * @typedef DogData
     * @property {number} age
     * @property {string} name
     */

    /**
     * @param {number} id The ID of the dog
     * @param {DogData} data The data of the dog
     */
    constructor (id, data) {
        super(id, data)
    }
}

const dog = new Dog(1, { age: 10, name: 'Foobar' })
dog.data. // Will only autocomplete if this.data is explicitly set inside the Dog constructor

I've noticed a problem when working with a parent class that's extended by a child class.
Both classes have a 'data' attribute, in the parent class it's a generic 'Object' type and in the child class it is of type 'DogData'.

The parent's constructor is setting 'this.data = data' which should override the type to whatever the child's attribute's type is, but this is not happening.

Please find a small reproducible example below:

I hope I'm missing something and please don't hesitate to point it out if I am.

Thanks in advance

 * The animal base class
 * @class
 */
class Animal {
    /**
     * @param {number} id The ID of the animal
     * @param {Object} data The data of the animal
     */
    constructor (id, data) {
        this.id = id
        this.data = data
    }
}

/**
 * The Dog class
 * @class
 * @extends Animal
 */
class Dog extends Animal {

    /**
     * @typedef DogData
     * @property {number} age
     * @property {string} name
     */

    /**
     * @param {number} id The ID of the dog
     * @param {DogData} data The data of the dog
     */
    constructor (id, data) {
        super(id, data)
    }
}

const dog = new Dog(1, { age: 10, name: 'Foobar' })
dog.data. // Will only autocomplete if this.data is explicitly set inside the Dog constructor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪 2025-02-10 08:14:45

在构造函数之前定义属性,然后在其上使用@Type

class Dog extends Animal {

    /**
     * @typedef DogData
     * @property {number} age
     * @property {string} name
     */

    /**
     * @type {DogData}
     */
    data;

    /**
     * @param {number} id The ID of the dog
     * @param {DogData} data The data of the dog
     */
    constructor (id, data) {
        super(id, data)
    }
}

Define the property before the constructor and use @type on it.

class Dog extends Animal {

    /**
     * @typedef DogData
     * @property {number} age
     * @property {string} name
     */

    /**
     * @type {DogData}
     */
    data;

    /**
     * @param {number} id The ID of the dog
     * @param {DogData} data The data of the dog
     */
    constructor (id, data) {
        super(id, data)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文