簡體   English   中英

JSDOC - typedef for function with sub function

[英]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.
 */

我很想正確地輸入這個,但我並沒有完全弄清楚如何做到這一點,而且文檔也沒有透露任何有用的信息。

也沒有在這里找到任何東西;)

您幾乎擁有所有正確的棋子; 缺少的鍵正在使用交叉類型。 調整你的“子功能”如下:

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

然后在你的$timeout typedef 上,執行以下操作:

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

這將創建cancel()作為$timeout的屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM