2017-10-16 119 views
1

我試圖在javascript中記錄輸入參數到一個函數,但我無法解決如何在jsdoc中執行它。jsdoc和vscode:記錄一個函數作爲參數傳遞給另一個函數

我看了一下jsdoc文檔,它建議使用@callback評論是必需的,但Visual Studio代碼(vscode)不會按照屏幕截圖來突出顯示它。

用於location參數的智能感知表明,它的類型any而非locator類型(具有id一個參數,它返回一個Location的函數)的。

shows the locater parameter not being type hinted

示例代碼顯示了一個函數調用作爲參數傳遞的函數:

class Location { 
    constructor(position, count) { 
    this.position = position; 
    this.count = count; 
    } 
} 

const items = { 
    'USB Cable': new Location('Desk Drawer', 123), 
    Keyboard: new Location('Desk Surface', 1), 
}; 

/** 
* A locater. 
* @param {string} id 
* @returns {Location} 
*/ 
const locaterA = id => items[id]; 

/** 
* Finds the item by its unique id. 
* @callback locater 
* @param {string} id 
* @returns {Location} 
*/ 

/** 
* Attempt to find the item with the given locater. 
* @param {string} id 
* @param {locater} locater 
*/ 
const locate = (id, locater) => locater(id); 

const result = locate('USB Cable', locaterA); 

console.log(result); 

這是與我在做什麼,不vsdoc配套使用的情況下,或vscode問題不支持它?

回答

1

Vscode的智能感知不支持@callback。它在這裏被追蹤:https://github.com/Microsoft/TypeScript/issues/7515

作爲方便的解決方法,你可以使用@typedef

/** 
* Finds the item by its unique id. 
* @typedef {function(string): Location} Locater 
*/ 

/** 
* Attempt to find the item with the given locater. 
* @param {string} id 
* @param {Locater} locater 
*/ 
const locate = (id, locater) => locater(id); 

enter image description here

1

它看起來像你正確使用它,每個JSDoc本身。但是,它看起來像Visual Studio可能只支持JSDoc的一個有限的子集,其中不包括@callbackhttps://msdn.microsoft.com/en-us/library/mt162307.aspx

我沒有Visual Studio方便,但你可以嘗試谷歌閉合風格,這是做它是這樣的:

@param { function(string) : number } locator 

這就是說,它是一個函數,需要一個字符串,並返回一個數字。

你可以在這裏找到該文檔:https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler(搜索「函數返回類型」跳轉到相關章節)。

我注意到JetBrains至少有東西,它支持這種語法。

相關問題