Как jsdo c описывает метод класса stati c, который возвращает переменную SAME CLASS - PullRequest
1 голос
/ 01 апреля 2020

Это очень простой пример того, что я хочу получить. Мой вопрос о теге @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

1 Ответ

0 голосов
/ 02 апреля 2020

Нет «относительного» типа. Вы можете довольно тривиально изменить расширенный класс;

/** @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();
  }
}
...