给出一个简单的自定义商店工厂功能
/**
* @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
*/
需要通过索引器访问属性,应该是这样的:
/**
* @typedef {Object} MyCustomStore
* @property {Readable<number>['subscribe']} subscribe
* @property {() => void} myMethod
*/