如何描述扩展方法何时返回此值?

时间:2018-10-14 17:17:50

标签: javascript jsdoc

我有一个返回this的方法。如何记录它,以便即使在子类中也能获得正确的类型?

如果我这样做:

class X {
  /** @return X */
  method () {
    return this
  }
}

class Y extends X {
  /** @type boolean */
  test = true
}

然后在这里

new Y().method().test

method被描述为X而不是Y的实例,因此它没有test属性。

应该看起来像

class X {
  /** @return {this} */ // <------ this
  method () {
    return this
  }
}

class Y extends X {
  /** @type boolean */
  test = true
}

class X {
  /** @return X */
  method () {
    return this
  }
}

/** @typedef {function():Y} Y.method */ // <------ this
class Y extends X {
  /** @type boolean */
  test = true
}

(如果相关,我会使用最新的WebStorm。)

0 个答案:

没有答案
相关问题