【问题标题】:JSDOC - typedef for function with sub functionJSDOC - 带有子函数的函数的 typedef
【发布时间】:2026-01-07 13:00:01
【问题描述】:

我目前对子函数的类型定义有以下问题。

示例代码:

/** @type {$timeout} */
const $timeout;
// this works with intellisense
const promise = $timeout(() => callback(1234), 999);
// this does not
const wasCanceled = $timeout.cancel(promise);

示例 jsdoc 类型定义:

/**
 * @typedef {_timeout} $timeout
 */
/**
 * @callback _timeout
 *
 * @param {function()=} fn A function, whose execution should be delayed.
 * @param {number=} [delay=0] Delay in milliseconds.
 * @param {boolean=} [invokeApply=true] 
 * @param {...*=} Pass additional parameters to the executed function.
 * @returns {Promise} Promise that will be resolved when the timeout is reached. The promise
 *   will be resolved with the return value of the `fn` function.
 */
/**
 * @callback _timeout#cancel
 *
 * @description
 * Cancels a task associated with the `promise`. As a result of this, the promise will be
 * resolved with a rejection.
 *
 * @param {Promise=} promise Promise returned by the `$timeout` function.
 * @returns {boolean} Returns `true` if the task hasn't executed yet and was successfully
 *   canceled.
 */

我很想正确输入此内容,但我并没有完全弄清楚该怎么做,文档也没有显示任何有用的信息。

这里也没有找到任何东西;)

【问题讨论】:

    标签: javascript jsdoc


    【解决方案1】:

    您几乎拥有所有正确的部分;缺少的键使用 intersection 类型。调整你的“子功能”如下:

    /**
     * @callback _cancelTask
     *
     * @description....
    

    然后在您的$timeout typedef 上,执行以下操作:

    /**
    * @typedef {{cancel: _cancelTask} & _timeout} $timeout
    */
    

    这将创建cancel() 作为$timeout 的属性

    【讨论】:

      最近更新 更多