在JSDOC中记录泛型类型参数 [英] Document generic type parameters in JSDOC

查看:350
本文介绍了在JSDOC中记录泛型类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSDoc 中,有可能记录数组内容的确切类型喜欢这样

In JSDoc there exists the possibility to document the exact types of array contents like this:

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

这使得像WebStorm这样的IDE中的代码完成实际上提供了正确的类型信息。 [0]。。这适用于Array类型,但是我有自己的集合类型,我也想使用这个功能。问题是我找不到合适的语法(可能因为没有,但是)。我希望能够以某种方式宣布我的课程:

This makes code completion in IDEs like WebStorm actually provide the right type information after the [0].. This works well for the Array type, however I have my own collection types where I would like to make use of this feature, too. The problem is I cannot find the right syntax (maybe because there is none, yet). I would love to be able to declare my class somehow like this:

/**
 * @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(){}

此语法或功能不适用于我的IDE,未列出此处,所以我想知道是否有语法对于此用例,可以是WebStorm或任何其他JS创作工具。

This syntax or feature does not work with my IDE and is not listed here, so I am wondering whether there is a syntax for this use-case, either for WebStorm or any other JS authoring tool.

推荐答案

同时,对此功能的支持已经已完成,现已记录在 Closure Compiler JSDOC页面上。

In the meantime, support for this feature has been finalized and is now documented on the Closure Compiler JSDOC page.

基本上是这样的:

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

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

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

不幸的是,在撰写本文时,WebStorm 7.0 不支持此功能(投票给它!),但是。

Unfortunately at the time of writing, WebStorm 7.0 does not support this feature (Vote for it!) , yet.

这篇关于在JSDOC中记录泛型类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆