我正在尝试使用JsDoc来记录es6类.无法相信您不能将类作为参数传递(类类型,而不是实例类型).
我一直在尝试但不能让这个简单的代码工作,以便JsDoc不会给我一些警告.
我不能让它工作,除非我为每个类创建一个@typedef,然后手动添加所有自己和继承的成员.甚至不能做混合!
有没有人成功传递构造函数/类参数?那么JsDoc是在静态上下文中,而不是实例上下文?
/**
* @class A
*/
class A {
/**
* @static
*/
static helloFromClassA(){
}
}
/**
* @class B
* @extends A
*/
class B extends A{
/**
* @static
*/
static helloFromClassB(){
}
}
/**
* Class as object
* @param {A} ClassArgument
*/
function fn1(ClassArgument){
ClassArgument.helloFromClassA(); // Unresolved function or method helloFromClassA
// Does not work because ClassArgument is interpreted as an
// instance of A, not A's constructor
}
/**
* // Class as function
* @param {Function} ClassArgument
*/
function fn2(ClassArgument){
ClassArgument.helloFromClassA(); // Unresolved function or method helloFromClassA
// Does not work because ClassArgument is interpreted as an
// empty function, not A's constructor
}
/**
* // Type definition
* @typedef {Object} AClass
* @property {Function} helloFromClassA
* @property {Function} super
*/
/**
* // Trying to mixin the AClass
* @typedef {Object} BClass
* @property {Function} helloFromClassB
* @mixes {AClass}
* @mixes {A}
*/
/**
* // Adding manually all members
* @typedef {Object} BClass2
* @property {Function} helloFromClassB
* @property {Function} helloFromClassA
*/
/**
* @param {BClass} ClassArgument
*/
function fn3(ClassArgument){
ClassArgument.helloFromClassA(); // Unresolved function or method helloFromClassA
// Does not work because the BClass typedef does not take
// into account the mixin from AClass, nor from A
ClassArgument.helloFromClassB(); // No warming
}
/**
* @param {BClass2} ClassArgument
*/
function fn4(ClassArgument){
ClassArgument.helloFromClassA(); // No Warning
ClassArgument.helloFromClassB(); // No warming
// Works because we manually defined the typedef with all own
// and inherited properties. It's a drag.
}
fn1(B);
fn2(B);
fn3(B);
fn4(B);
jsDoc问题:https://github.com/jsdoc3/jsdoc/issues/1088
解决方法:
多次在WebStorm中遇到自动完成问题.虽然看起来目前在jsdoc中没有直接的方式来说该参数是对构造函数的引用(而不是对实例),但JetBrains团队建议实现类似@param {typeof Constructor}(其中) typeof来自typescript)或@param {Constructor.},由闭包编译团队提供,是suggested.您可以投票支持以下问题,以解决您对WebStorm自动完成功能的主要关注 – https://youtrack.jetbrains.com/issue/WEB-17325