简体   繁体   English

JSDoc 这就是您将@typedef 标记为@global 的方式吗?

[英]JSDoc is this how you mark a @typedef as @global?

Just making sure that this in a module in our Node server is the right way to be able to use an @typedef throughout the application instead of repeating it in every module/file it is needed.只需确保在我们的 Node 服务器的模块中使用@typedef是正确的方法,以便能够在整个应用程序中使用@typedef而不是在需要的每个模块/文件中重复它。 From the docs I can't determine if this is correct or not, and does anyone have an opinion on where to store global @typedefs so they are easy to find if they need to be changed when used throughout the application.从文档中我无法确定这是否正确,并且是否有人对全局@typedef 的存储位置有意见,以便在整个应用程序中使用时是否需要更改它们很容易找到。

/**
 * Universally unique identifier.
 * @global
 * @typedef {string} UUID
 */

A bit of a late answer here, but I came across this issue from Google, so this is how I solved this issue for myself.这里的答案有点晚,但我从谷歌遇到了这个问题,所以这就是我自己解决这个问题的方法。 Hopefully it helps future people!希望对未来的人有帮助!

I ran into a similar situation where I had a typedef in a module and wanted to use that type elsewhere in the application, without redefining the type.我遇到了类似的情况,我在一个模块中有一个typedef ,并想在应用程序的其他地方使用该类型,而无需重新定义该类型。

I went with something like this:我去了这样的事情:

myModule.js
/**
 * Universally unique identifier.
 * @typedef {string} UUID
 */
myOtherScript.js

/**
 * @function
 * @param {import('path/to/myModule.js').UUID} uuid
 */
function getByUUID(uuid) { ... }

This doesn't make the typedef strictly global, so you cannot do @param {UUID} uuid (which would be cleaner, however I've yet to find a way to achieve this) but this approach worked for my use-case, and definitely is better than rewriting the type everywhere it is used.这不会使typedef严格全局化,因此您不能执行@param {UUID} uuid (这更清晰,但是我还没有找到实现此目的的方法)但是这种方法适用于我的用例,并且绝对比在任何使用它的地方重写类型都要好。

More info around this topic can be found in this Github issue .可以在此 Github 问题中找到有关此主题的更多信息。

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

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