2014-05-21 51 views
0

說我有一個字符串數組。JSDoc類型等於數組的成員

/** @type Array.<string> */ 
var possibleValues = ['a','b','c','d']; 

我有一個接受字符串的函數,但我只希望它是等於possibleValues成員之一。

/** 
* It does nothing. 
* @param {string} input One of the members of `possibleValues`. 
* @returns {string} Same as your input, useless... or is it? 
*/ 
function nothing(input) { return input; } 

是否有JSDoc 3一種方法,我可以在input參數的類型明確設定爲possibleValues的一個值?

最好我想引用原始數組,而不僅僅是在評論中列出其成員,因爲我可能會重用它,它可能會稍後進行更改。

+0

我根據你的措辭,該陣列是不是在運行時更改猜測(否則我也沒指望你提爲「列表中的成員」因爲靜態的值列表會導致誤導),但我不能100%確定我是對的。 – Louis

+0

@Louis它在運行時不會改變,通過_「可能改變」_我的意思是在未來版本的應用程序中。 –

+0

可能是'@ typedef'標記(http://usejsdoc.org/tags-typedef.html)可以幫助您引用它。 – krampstudio

回答

1

我會使用@enum此:

/** 
* @readonly 
* @enum {string} 
*/ 
var possibleValues = { 
    A: 'a', 
    B: 'b', 
    C: 'c', 
    D: 'd' 
}; 

/** 
* It does nothing. 
* @param {possibleValues} input Something descriptive... 
* @returns {string} Same as your input, useless... or is it? 
*/ 
function nothing(input) { return input; }