gpt4 book ai didi

javascript - JsDoc、ES6 和@param {Constructor}

转载 作者:数据小太阳 更新时间:2023-10-29 06:15:54 28 4
gpt4 key购买 nike

我正在尝试使用 JsDoc 来记录 es6 类。无法相信您不能将类作为参数传递(类类型,而不是实例类型)。

我一直在尝试一些事情,但无法让这个简单的代码正常工作,因此 JsDoc 不会向我抛出一些警告。

除非我为我的每个类创建一个@typedef,然后手动将所有自己的和继承的成员添加到它,否则我无法让它工作。甚至不能做 mixin!

有没有人成功传递构造函数/类参数?让 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

关于javascript - JsDoc、ES6 和@param {Constructor},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33150924/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com