JSDOC - 带有子函数的函数的类型定义

JSDOC - typedef for function with sub function

我目前在子函数的类型定义方面存在以下问题。

示例代码:

/** @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.
 */

我很想正确输入此内容,但我不太清楚如何输入,而且文档也没有透露任何有用的信息。

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

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

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

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

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

这将创建 cancel() 作为 $timeout

的 属性