2015-10-15 46 views
4

我想使用JsDoc来记录es6类。不能相信你不能将类作为参数传递(类类型,不是实例类型)。JsDoc,ES6和@param {构造函数}

我一直在尝试的东西,但不能得到这个简单的代码工作,使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

+0

你声明的那些方法作为'静态' - 在实例上调用它们会起作用,但是对于关键字来说这有点奇怪。 – CodingIntrigue

+1

你是什么意思? ES6有static关键字,可以让你在构造函数而不是原型上声明方法。这些是**静态**方法。而且我不是试图在实例上调用它们,我正在调用构造函数。这里的问题是JsDoc不允许你声明一个参数作为构造函数(一种类型),它只允许你声明一个参数作为一个实例。 – Ludo

+0

我明白了。我错误地认为val是一个实例,而不是对函数的引用 – CodingIntrigue

回答

1

我一直运行到与WebStorm多次自动完成同样的问题。虽然目前看起来jsdoc中没有直接的方式说参数是对构造函数的引用(而不是实例),但JetBrains团队提供了一个建议来实现类似@param {typeof Constructor}(其中typeof来自typescript)或@param {Constructor。},它是封闭编译器团队的suggested。您可以投票选出以下问题,让您与WebStorm自动完成解决主要关注 - https://youtrack.jetbrains.com/issue/WEB-17325

1

在Visual Studio代码,你可以指定

@param {function(new:MyClass, SomeArgType, SecondArgType, etc...)} 

我不知道什么是规范的语法来自要不谁支持它,但它适用于我。