【问题标题】:How to use Jsdoc to describe an extended class's property that is defined in super class如何使用 Jsdoc 描述超类中定义的扩展类的属性
【发布时间】:2020-07-15 21:31:10
【问题描述】:

我想做的是这样的:

class A {
  someMethod() {
    //
  }
}

class B {
  constructor (options) {
    this.dependency = options.dependency;
  }
}


class C extends B {
  test() { 
    this.dependency.a. // want a to refer to instance of A class and provide intellisense using jsdocs
  }
}

我在普通对象上尝试了一些东西,它可以工作,但我无法让它在类上工作或覆盖某些属性。 这是我尝试过的:

/**
 * @typedef {Object} WithSomeProp
 * @property {{ a: A }} prop
 * 
 * @typedef {C & WithSomeProp} extended
 */

/**
 * @type {extended}
 */
let a = {};
a.prop.a.someMethod // works (shows suggestion)
a.test // works

/**
 * @typedef {Object} WithOtherProp
 * @property {{ a: A }} dependency
 * 
 * @typedef {C & WithOtherProp} extendedTwo
 */

/**
 * @type {extendedTwo}
 */
let b = {};
b.dependency. // doesn't work
b.test // works

所以总体而言,没有任何方法可以稍后在扩展类上以某种方式覆盖属性定义以获得智能感知支持?

【问题讨论】:

    标签: visual-studio-code intellisense jsdoc es6-class javascript-intellisense


    【解决方案1】:

    我刚刚意识到我可以这样做(我现在感觉很愚蠢)

    class C extends B {
      constructor(options) {
        super(options);
        /**
         * @type {A}
         */
        this.a = options.dependency.a;
      }
      test() { 
        this.a.someMethod // works fine now
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多