Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic @returns in JSDoc, based on @param value

Is there a way to document the function, so vscode-intellisense knows that getObject("player") returns a Player type and getObject("bullet") returns a Bullet type?

/**
 * @param  {string} type
 * @return {????}
 */
function getObject(type) {
    switch (type) {
        case 'player': return new Player();
        case 'bullet': return new Bullet();
    }
}

An example of this functionality would be document.createElement(...).

document.createElement("canvas") => intellisense knows it's a HTMLCanvas type.

like image 976
blaz pecnik Avatar asked Nov 28 '25 18:11

blaz pecnik


1 Answers

You cannot do this with jsdocs.

document.createElement uses typescript function overload definitions for its behavior. You could also use a *.d.ts this way:

declare function getObject(x: 'player'): Player;
declare function getObject(x: 'bullet'): Bullet;
like image 75
Matt Bierner Avatar answered Nov 30 '25 07:11

Matt Bierner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!