簡體   English   中英

如何指定繼承類的模板類型?

[英]How to specify template type of inherited class?

考慮一個泛型集合:

/**
 * 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假設items上繼承類Item[]沒有TSingleItem[]

我在課堂上試過這個:

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

至少在視覺工作室中沒有幫助。 什么是正確的語法? 如果它適用於 Visual Studio 智能感知,則可以獲得加分。

事實證明這是正確的代碼,也可以在這個不錯的教程中看到:

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

它也適用於 Visual Studio 2017,只是需要一段時間才能流行起來。 我要離開這個問答而不是刪除,因為谷歌確認它並不明顯或容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM