【问题标题】:JSDoc - defining detailed return type for array of predefined typesJSDoc - 为预定义类型数组定义详细的返回类型
【发布时间】:2020-02-25 05:38:16
【问题描述】:

TLDR;我该如何翻译:

/** @return {((function(...[*]=))|{})[]} */

进入自己的@typedef


我正在尝试使用 JSDoc 在 @typedef 中正确定义从我的函数返回的值,如果不使用 @return {((function(...[*]=))|{})[]} * 替换我的类型,我无法正确定义它。

我目前拥有的:

/**
 * @typedef {Array} MyType
 * @property {function(): void} submit - Submit
 * @property {Object} state - FetchResult
 */

/** @return {MyType} */
function fooBar() {
    return [() => {}, {}];
}

导致警告:

Returned expression type ((function(...[*]=)) | {})[] is not assignable to type MyType

fooBar() 的 JSDoc 替换为:

/** @return {((function(...[*]=))|{})[]} */

导致正确的行为。

感谢您的帮助,

【问题讨论】:

    标签: javascript jsdoc


    【解决方案1】:

    您所做的是记录 Array 类型本身的属性,而不是数组中的值:

    /**
     * @typedef {Array} MyType
     * @property {function(): void} submit - Submit <--- MyType.submit?
     * @property {Object} state - FetchResult       <--- MyType.state?
     */
    

    可能想要做的是数组索引的命名标签:

    /**
     * @typedef {Array} MyType
     * @property {Function} 0 - Submit function
     * @property {Object} 1 - state
     */
    

    但是,judging by this currently-open issue for jsdocs 我想说这并不能完全解决问题。 fooBar 中的返回表达式可能仍会突出显示,表示 "*[] is not assignable to type MyType" 即使您有 @returns {MyType} 记录函数。

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 2022-10-12
      • 2020-12-30
      • 2023-03-15
      • 2017-11-21
      • 2016-04-24
      • 1970-01-01
      相关资源
      最近更新 更多