admin管理员组

文章数量:1289529

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/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?

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

Share Improve this question asked May 26, 2020 at 14:15 Daniel ReinaDaniel Reina 6,3975 gold badges40 silver badges57 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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() {};

本文标签: javascriptHow to document the type of a global variable with JSDOCStack Overflow