javascript - jsdoc如何描述返回SAME CLASS变量的静态类方法

标签 javascript jsdoc

这是我想要得到的非常简单的例子。我的问题是关于@returns 标签。我应该在那里写什么?

class Base{
  /**
  * @returns {QUESTION: WHAT SHOUL BE HIRE??}
  */
  static method(){
    return new this()
  }
}

class Sub extends Base{
}

let base= Base.method() // IDE should understand that base is instance of Base
let sub= Sub.method() // IDE should understand that sub is instance of Sub

最佳答案

没有“相对”类型。 您可以相当简单地修改扩展类;

/** @extends {Base} */
class Sub extends Base{
  /**  @returns {Sub} */
  static method(){
    return super.method();
  }
}

或者也许使用第三种类型,一个定义此方法存在的@interface

/** @interface */
class I {
  method() {}
}
/** @implements {I} */
class Base {
  /**  @returns {I} */
  static method(){
    return new this();
  }
}

/** 
 * @extends {Base}
 * @implements {I}
 */
class Sub {
  /**  @returns {I} */
  static method(){
    return new this();
  }
}

关于javascript - jsdoc如何描述返回SAME CLASS变量的静态类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60980163/

相关文章:

javascript - jQuery 的 IE GIF/PNG 透明度问题

jsdoc : reference typedef-ed type from other module

linux shell 将可变参数附加到命令

visual-studio - 参数属性声明的 JSDoc 不适用于属性

javascript - 有 JSDoc 标准吗?

javascript - 如何设置绝对位置div居中对齐

javascript - 启动和停止计时器 : using 1 function for multiple instances

reactjs - 使用React.fowardRef和useImperativeHandle时如何创建ref的JSDoc?

javascript - 由于生成了错误的路径,图像在 React 的生产版本中消失了

JavaScript JQuery 隐藏/显示功能