【问题标题】:JSDoc: document generic type that works for grandchildren classesJSDoc:适用于孙子类的文档泛型类型
【发布时间】:2018-12-07 21:22:47
【问题描述】:

记录泛型类型适用于直接继承。但是当我有一个继承链时,没有办法让它为孙子类工作。这是一个例子:

 * @property {string} color
 * @template {T}
 */
class MyColor {
  constructor() {
    this.color = 'unknown';
  }

  /**
   * @returns {T}
   */
  static makeColor() {
    return /**@type {T}*/ new this.prototype.constructor();
  }
}

/**
 * @extends MyColor<Red>
 * @template {T}
 */
class Red extends MyColor {
  constructor() {
    super();
    this.color = 'red';
  }
}

/**
 * @extends Red<DarkRed>
 */
class DarkRed extends Red {
  constructor() {
    super();
    this.level = 2;
  }

  darker() {
    this.level += 1;
  }
}

const circle = DarkRed.makeColor();

DarkRed.makeColor 仅将返回识别为Red,但不识别DarkRed。有没有办法让它与@template 一起工作?或者有没有其他方法可以让它工作?

我使用 WebStorm 作为 IDE。

【问题讨论】:

    标签: node.js webstorm jsdoc


    【解决方案1】:

    来自https://github.com/google/closure-compiler/wiki/Generic-Types#inheritance-of-generic-types@extends MyColor&lt;Red&gt;“修复”模板类型,而不是将其传播到继承类型。例如,在

    /**
     * @constructor
     * @template T
     */
    var A = function() { };
    
    /** @param {T} t */
    A.prototype.method = function(t) {  };
    
    /**
     * @constructor
     * @extends {A<string>}
     */
    var B = function() {  };
    
    /**
     * @constructor
     *
     * @extends {B<number>}
     */
    var C = function() {  };
    
    
    var cc =new C();
    var bb = new B();
    
    var bm = bb.method("hello");
    var cm = cc.method(1);
    

    cc.method(1) 将导致TYPE_MISMATCH: actual parameter 1 of A.prototype.method does not match formal parameter found : number required: string

    您可以尝试将代码更改为

    /**
    * @property {string} color
     * @template {T}
     */
    class MyColor {
      constructor() {
        this.color = 'unknown';
      }
    
      /**
       * @returns {T}
       */
      static makeColor() {
        return /**@type {T}*/ new this.prototype.constructor();
      }
    }
    
    /**
     * @extends MyColor<T>
     * @template {T}
     */
    class Red extends MyColor {
      constructor() {
        super();
        this.color = 'red';
      }
    }
    
    const circle1 = Red.makeColor();
    
    /**
     * @extends Red<DarkRed>
     *
     */
    class DarkRed extends Red {
      constructor() {
        super();
        this.level = 2;
      }
    
      darker() {
        this.level += 1;
      }
    }
    
    const circle = DarkRed.makeColor();
    

    另一种可能的解决方案是使用 @return {this} 而不是 @template(从 2018.2 开始工作):

    class MyColor {
      constructor() {
        this.color = 'unknown';
      }
    
      /**
       * @return {this}
       */
      static makeColor() {
        return  new this.prototype.constructor();
      }
    }
    
    
    class Red extends MyColor {
      constructor() {
        super();
        this.color = 'red';
      }
    }
    
    class DarkRed extends Red {
      constructor() {
        super();
        this.level = 2;
      }
    
      darker() {
        this.level += 1;
      }
    }
    

    【讨论】:

    • 这适用于DarkRed,但Red.makeColor() 不会将返回识别为Red
    • 我猜你不能修复类型并同时使用 JSDoc 将其传播到继承类型:(
    • 那我猜这是 JSDoc 的限制,而不是 WebStorm。你认为有没有其他方法可以在不使用模板的情况下使用 JSDoc 来做到这一点?
    • 您可以删除所有 JSdoc cmets 并使用 /** * @return {this} */ static makeColor() {};但 htis 仅适用于 2018.2 (youtrack.jetbrains.com/issue/WEB-33061)
    • 请更新您的答案,我会接受。谢谢@lena
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 2023-02-11
    相关资源
    最近更新 更多