在函数的@param和@returns中使用相同的泛型类型

时间:2018-08-09 15:53:10

标签: javascript generics jsdoc

如何用JSDoc注释接受和返回相同 类型对象的function?类似于以下内容:

/** 
* 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    
}

有可能吗?

1 个答案:

答案 0 :(得分:1)

解决方案是指定@template T

所以看起来像这样:

/** 
* 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    
}
相关问题