简体   繁体   English

如何使用 JSDOC 记录全局变量的类型

[英]How to document the type of a global variable with JSDOC

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.有一个从 CDN 加载的库,它将一些变量放在全局 scope 上。 How can I document the type of that variable?如何记录该变量的类型?

For example例如

index.hmtl => puts globalVariable on the global scope index.hmtl => 将globalVariable放在全局 scope

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

index.js => uses globalVariable index.js => 使用globalVariable

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

Something like that so I can specify the type of globalVariable .类似的东西,所以我可以指定globalVariable的类型。 Is that possible?那可能吗?

Typecasting and the window global can be your friend here.类型转换和window全局可以成为您的朋友。

To cast:投:

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

To modify the window global add an externs file that contains:要修改window全局,请添加一个包含以下内容的外部文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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