我有一个分页控制器工厂,该工厂返回带有多种方法的分页控制器Object(用于与视图进行交互,尤其是当最终用户执行诸如导航到其他页面或输入一些搜索文本的操作时) 。它的定义类似于:
/**
* Returns a paging controller object with data
* @param {Object[]} data
* @param {string} prop the property containing that data. If it's a function, it should be no-args.
* @param {filterFunc} filterer a callback that filters the data
*/
function pagingControllerFor(data, prop, filterer) {
let _currentPage = 0
let _filterFunc = filterer
let _stateChange = false
let _data;
const _ITEMS_PER_PAGE = 50
let _selectAllChecked = [];
/**
* Getter for all the data. Useful for debugging.
*/
function getAllData() {
if (prop) {
if (typeof data[prop] === 'function') {
return data[prop]()
}
return data[prop]
}
return data
}
/**
* Always returns fresh data for the controller
*/
function getData() {
let data = getAllData()
if (_filterFunc) {
if ((_stateChange) || (!_data)) {
_data = data.filter(_filterFunc)
_selectAllChecked = Array(Math.ceil(_data.length / _ITEMS_PER_PAGE)).fill(false)
_stateChange = false
}
return _data
}
return data
}
return {
/* a whole bunch of methods irrelevant to my use case on here */
getCurrentPageData : () => getData().slice(_currentPage * _ITEMS_PER_PAGE, (_currentPage + 1) * _ITEMS_PER_PAGE),
// get/set current "Select All" checkbox state
isCurrentSelectAllChecked : () => _selectAllChecked[_currentPage],
setCurrentSelectAllChecked : (checked) => _selectAllChecked[_currentPage] = checked
}
}
我正在为要分页的视图上的“全选/取消全选”复选框编写事件绑定器。截至我撰写本文时,它定义为:
/**
* Binds clicks on the current "Select/Deselect All" checkbox to the controller
* @param {string} modalType
* @param {{ getCurrentPageData : () => Array<{IsSelectedOnModal : boolean}>, setCurrentSelectAllChecked : () => boolean }} controller
* @param {Function} callback
*/
function bindToggleSelectAllEvent(modalType, controller, callback) {
callback = callback || bindToggleSelectAllEvent
const modalSelector = `#${modalType}-selector-modal`
$(`#toggle-all-${(modalType === ITEM) ? 'items' : 'categories'}-selected`)
.off('change')
.on('change', function() {
// get the state of this
let isChecked = $(this).prop('checked')
// change the selection state of all current items/categories in the controller to that state
controller.getCurrentPageData().forEach((data) => {
data.IsSelectedOnModal = isChecked
})
// tell the controller the new state of this "Select All" checkbox
controller.setCurrentSelectAllChecked(isChecked)
// Re-render modal?!
// TODO: implement this
})
}
VSCode知道我在做什么,因为它检测到我指定的controller
的相关方法。
但是,出于某些原因,JSDoc却没有:
ERROR: Unable to parse a tag's type expression for source file [my-project-path]\static\js\menu\edit\index.js in line 433 with tag title "param" and text "{{ getCurrentPageData : () => Array<{IsSelectedOnModal : boolean}>, setCurrentSelectAllChecked : () => boolean }} controller": Invalid type expression "{ getCurrentPageData : () => Array<{IsSelectedOnModal : boolean}>, setCurrentSelectAllChecked : () => boolean }": Expected "," or "}" but "=" found.
ERROR: Unable to parse a tag's type expression for source file [my-project-path]\static\js\menu\edit\index.js in line 439 with tag title "param" and text "{{ getCurrentPageData : () => Array<{IsSelectedOnModal : boolean}>, setCurrentSelectAllChecked : () => boolean }} controller": Invalid type expression "{ getCurrentPageData : () => Array<{IsSelectedOnModal : boolean}>, setCurrentSelectAllChecked : () => boolean }": Expected "," or "}" but "=" found.
我该怎么办?
答案 0 :(得分:3)
VS Code在JS文档中支持TypeScript类型,但JS Doc工具仅支持Closure types。
我认为您使用的箭头函数类型表达式是有效的TypeScript类型,但JSDoc工具无法理解。尝试改用function():
function type syntax
@param {{ getCurrentPageData : function(): Array<{IsSelectedOnModal : boolean}> }} controller