【问题标题】:How to specify template type of inherited class?如何指定继承类的模板类型?
【发布时间】:2020-06-26 06:00:22
【问题描述】:

考虑一个通用集合:

/**
 * A collection of items of the same type
 * @template TSingleItem
 * */
class ItemsCollection {
    /** @type {TSingleItem[]} **/
    get items() {
        return [createSingleItem()];
    }
    /**
     * Creates a new item based on the implementation type
     * @param {string} param
     * @returns {TSingleItem}
     */
    createSingleItem(param) {
        throw new Error("Pure virtual method call!");
    }
}

现在我们将其实现为:

class Item {
    constructor(name) {
        this.name = name;
    }
}

class Items extends ItemsCollection {
    createSingleItem(param) {
        return new Item(param);
    }
}

我如何告诉 JSDoc 假设该继承类上的 itemsItem[] 而不是 TSingleItem[]

我在课堂上试过这个:

/**
 * @extends {ItemsCollection<Item>}
 * */
class Items extends ItemsCollection

至少在视觉工作室中没有帮助。什么是正确的语法?如果它与 Visual Studio intellisense 一起使用,则会获得奖励积分。

【问题讨论】:

    标签: javascript ecmascript-6 jsdoc


    【解决方案1】:

    原来这是正确的代码,也可以看到in this nice tutorial

    /**
     * @extends {ItemsCollection<Item>}
     * */
    class Items extends ItemsCollection
    

    它也可以在 Visual Studio 2017 中使用,只是需要一段时间才能掌握。我将留下此问答,而不是删除,因为它不是很明显或很容易通过谷歌来确认。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      相关资源
      最近更新 更多