dbardakov
dbardakov

Reputation: 699

Use of same generic type in @param and @returns for a function

How to annotate with JSDoc comment a function that accepts and returns object of the same type? Something like the following:

/** 
* Does some work and returns same type
* @param {T extends Object} src Source object 
* @returns {T} object of the **same** type
*/
function chainFoo(src) {
  // do some work
  return Object.assing({}, src); // just as example    
}

Is it possible?

Upvotes: 2

Views: 1006

Answers (1)

dbardakov
dbardakov

Reputation: 699

The solution is to specify @template T

So it looks like this:

/** 
* Does some work and returns same type
* @template T
* @param {T} src Source object 
* @returns {T} object of the **same** type
*/
function chainFoo(src) {
  // do some work
 return Object.assing({}, src); // just as example    
}

Upvotes: 7

Related Questions