简体   繁体   中英

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. 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

Of course the line 4 is completely wrong

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. See this for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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