简体   繁体   English

jsDoc:如何使我的方法的@return 类型成为它的局部变量的类型?

[英]jsDoc : How do I make the @return type of my method the type of a local variable from it?

I'm making a javascript project and I want to make it as clean as possible, and for that I'm using jsDoc at the top of my methods.我正在制作一个 javascript 项目,我想让它尽可能干净,为此我在我的方法顶部使用 jsDoc。 For one of them, I would like to know if it's possible to specify as a return type the type of a local variable ?对于其中之一,我想知道是否可以将局部变量的类型指定为返回类型?

Like this :像这样 :

/**
* Add a component to the gameObject
* @param {Function} component to add
* @return {comp.constructor.name}
*/
addComponent(component){
    let comp = eval("new Tzu." + component.name + "({gameObject : this});");
    this.behaviors.push(comp);

    return comp;
} 

I want the @return to be the type of comp我希望 @return 是comp的类型

Of course the line 4 is completely wrong当然第4行是完全错误的

Thanks !谢谢 !

You should be able to do something like你应该能够做类似的事情

let test = 'test';

/**
 * @returns {typeof test}
 */
function doSomething(test) {
    return test;
}

As a side note, you really shouldn't use eval.作为旁注,你真的不应该使用 eval。 See this for more information.有关更多信息,请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM