How to document the type of a global variable with JSDOC

Viewed 3647

I have a piece of code that runs on the browser. There's a library loaded from a CDN that puts some variables on the global scope. How can I document the type of that variable?

For example

index.hmtl => puts globalVariable on the global scope

...
<script src="//cdn.library.com/library.js"></script>
...

index.js => uses globalVariable

/**
 * @type {SomeType} globalVariable
 */
const foo = globalVariable()

Something like that so I can specify the type of globalVariable. Is that possible?

1 Answers

Typecasting and the window global can be your friend here.

To cast:

const globalVariable = /** @type {someType} */ (window.globalVariable);

To modify the window global add an externs file that contains:

/** @type {someType} */
window.prototype.globalVariable = function() {};
Related