最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSDoc is this how you mark a @typedef as @global? - Stack Overflow

programmeradmin2浏览0评论

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

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

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

/**
 * Universally unique identifier.
 * @global
 * @typedef {string} UUID
 */
Share Improve this question edited Sep 27, 2017 at 20:55 mtpultz asked Sep 26, 2017 at 22:51 mtpultzmtpultz 18.3k24 gold badges127 silver badges210 bronze badges 3
  • Did you ever determine if this was correct? – Matthew Herbst Commented Mar 19, 2018 at 20:59
  • No, but I ended up using it anyway since the code needed documentation, and this was how the docs appeared to steer its usage. Even now no one has changed it so my team doesn't have any better idea of the proper use, but it feels like it provides clarity, and is easy to determine the intent, which might be enough. – mtpultz Commented Mar 19, 2018 at 21:04
  • Thanks for the quick response. Guess I'll use it too! Hopefully we can get an "official" answer to this someday - I just scoured the @global and @typedef docs again + Google to no avail :( – Matthew Herbst Commented Mar 19, 2018 at 21:09
Add a ment  | 

1 Answer 1

Reset to default 9

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.

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.

More info around this topic can be found in this Github issue.

发布评论

评论列表(0)

  1. 暂无评论