Home > Back-end >  jsDoc : How do I make the @return type of my method the type of a local variable from it?
jsDoc : How do I make the @return type of my method the type of a local variable from it?

Time:06-01

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 !

CodePudding user response:

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.

  • Related