2013-04-15 24 views
7

In JSDoc esiste la possibilità di documentare l'esatto tipi di contenuto dell'array like this:Document parametri di tipo generico in JSDOC

/** @param {Array.<MyClass>} myClasses An array of MyClass objects. */ 
TestClass.protoype.someMethod = function(myClasses){ 
    myClasses[0].aMethodOnMyClass(); 
} 

Questo rende completamento del codice in IDE come WebStorm effettivamente fornire le informazioni tipo giusto dopo la [0].. Funziona bene per il tipo di array, tuttavia ho i miei tipi di raccolta personali in cui vorrei utilizzare anche questa funzione. Il problema è che non riesco a trovare la sintassi corretta (forse perché non ce ne sono ancora). Mi piacerebbe essere in grado di dichiarare la mia classe in qualche modo simile a questo:

/** 
* @typeparam {T} the type parameter 
* @constructor {Test2.<T>} 
* */ 
Test2 = function(){}; 

/** 
* @returns {T} a value of type T, where T is the generic type parameter of Test2 
*/ 
Test2.prototype.getGenericValue = function(){} 

Questa sintassi o funzionalità non funziona con il mio IDE e non è elencato here, quindi mi chiedo se esiste una sintassi per questo uso- caso, sia per WebStorm che per qualsiasi altro strumento di authoring JS.

risposta

4

Nel frattempo, il supporto per questa funzione è stato finalizzato ed è ora documentato su the Closure Compiler JSDOC page.

In sostanza è come questo:

/** 
* @constructor 
* @template T 
*/ 
Foo = function() { ... }; 

e

/** @return {T} */ 
Foo.prototype.get = function() { ... }; 

/** @param {T} t */ 
Foo.prototype.set = function(t) { ... }; 

Purtroppo al momento della scrittura, WebStorm 7,0 does not support this feature (Vote for it!), ancora.

11

Puoi provare a utilizzare il tag @template (tag non documentato utilizzato nella libreria di Google Closure - forma estremamente limitata di generici). Qualcosa di simile:

/** 
* Search an array for the first element that satisfies a given condition and 
* return that element. 
* @param {Array.<T>|goog.array.ArrayLike} arr Array or array 
*  like object over which to iterate. 
* @param {?function(this:S, T, number, ?) : boolean} f The function to call 
*  for every element. This function takes 3 arguments (the element, the 
*  index and the array) and should return a boolean. 
* @param {S=} opt_obj An optional "this" context for the function. 
* @return {T} The first array element that passes the test, or null if no 
*  element is found. 
* @template T,S 
*/ 
goog.array.find = function(arr, f, opt_obj) {  
    var i = goog.array.findIndex(arr, f, opt_obj);  
    return i < 0 ? null : goog.isString(arr) ? arr.charAt(i) : arr[i]; 
}; 

WebStorm utilizza questo tag per tipo di hinting - cioè se passiamo array di stringhe per goog.array.find nel campione di cui sopra, IDE saprà che tipo di ritorno è una stringa, quindi le opzioni di completamento stringa essere suggerito ecc.

Non so se questo è quello che stai cercando ... Il post che sembra collegato è here.

+0

Grazie, ho appena trovato questo fuori me stesso da [questo problema YouTrack] (http://youtrack.jetbrains.com/issue/WEB- 1208) e [questo changeset del compilatore di chiusura] (https://code.google.com/p/closure-compiler/source/detail?spec=svn64d22457ddca24b07370f711276a449273bd6330&r=0eb41cabc9ba5463e3a34ea2fd900a8dd54f2136). C'è supporto per questo a livello di classe? I miei test indicano che questo funziona solo per "parametri di tipo locale della funzione". – Sebastian

0

Il seguente codice funziona bene per me in WebStorm 8.

/** @type {Array.<MyPair.<Event, Array.<Thought>>>} */ 
scope.pairs = []; 

/** 
* @template TFirst, TSecond 
*/ 
function MyPair(first, second){ 
    this.first = first; 
    this.second = second; 
} 
/** @type {TFirst} */ 
MyPair.prototype.first = null; 
/** @type {TSecond} */ 
MyPair.prototype.second = null; 

... 
function Event(){} 
... 
... 
function Thought(){} 
... 
+0

Nell'esempio precedente, ha funzionato anche con oggetti Event e Thought. –

+1

Sì, nel frattempo molto di questo è stato implementato in WS8, ma ci sono ancora problemi aperti che verranno risolti solo in WS9 - vedere la mia risposta e i problemi collegati. – Sebastian

Problemi correlati