如何用JSDoc输入自定义商店的订阅方法

问题描述 投票:0回答:1

给出一个简单的自定义商店工厂功能

/**
 * @template T generic data to handle in store
 * @typedef {import('svelte/store').Readable<T>} Readable
 */

/**
 * @typedef {Object} MyCustomStore
 * @property {Readable.subscribe} subscribe
 * @property {() => void} myMethod
 */
/**
 *@returns {MyCustomStore}
 */
function createMyCustomStore() {
    const { subscribe, set, update } = writable(0);
    return {
        subscribe,
        myMethod() {
            console.log('...');
        },
    };
}

如何正确输入订阅方法?

Readable.subscribe
给出

无法访问 Readable.subscribe,因为 Readable 是一种类型,而不是命名空间。

由于

Readable
也只有 subscribe 方法,我想知道
MyCustomStore
类型是否可以扩展它,但随后就找不到该属性了

/**
* @typedef {Object} MyCustomStore
* @extends Readable<number>
* @property {() => void} myMethod
*/
svelte jsdoc
1个回答
0
投票

需要通过索引器访问属性,应该是这样的:

/**
 * @typedef {Object} MyCustomStore
 * @property {Readable<number>['subscribe']} subscribe
 * @property {() => void} myMethod
 */
© www.soinside.com 2019 - 2024. All rights reserved.