JSDOC不拾取数据子属性类型覆盖
我注意到与子班延长的父母课程一起工作时有一个问题。 这两个类都有一个“数据”属性,在父类中,它是一种通用的“对象”类型,在子类中,它是类型为“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在构造函数之前定义属性,然后在其上使用
@Type
。Define the property before the constructor and use
@type
on it.