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

javascript - How to document a JSON parameter in JSDoc? - Stack Overflow

programmeradmin2浏览0评论

I've just started learning JSDoc and the first stumbling block I've e across is how to indicate a function parameter which must be valid JSON.

I'm looking at the documentation for @param and I can't see:

  • @param {JSON}

What's the correct approach in JSDoc to indicate a function parameter which must be valid JSON?

(Surely it's not right to use @param {string}, is it?)

I've just started learning JSDoc and the first stumbling block I've e across is how to indicate a function parameter which must be valid JSON.

I'm looking at the documentation for @param and I can't see:

  • @param {JSON}

What's the correct approach in JSDoc to indicate a function parameter which must be valid JSON?

(Surely it's not right to use @param {string}, is it?)

Share Improve this question edited Dec 12, 2021 at 21:20 Rounin asked Dec 12, 2021 at 19:26 RouninRounin 29.6k13 gold badges98 silver badges123 bronze badges 2
  • 2 JSON text is a string. JsDoc has no syntax to document the format of strings, you need to use the textual description of the parameter for that. – Bergi Commented Dec 12, 2021 at 19:43
  • @Bergi Perhaps I can deploy something like: @typedef {string} JSON and then use @param {JSON}? (See: jsdoc.app/tags-typedef.html) – Rounin Commented Dec 12, 2021 at 19:50
Add a ment  | 

1 Answer 1

Reset to default 5

In the absence of any other answers so far, I'm going to go with defining JSON as a type at the top of the javascript file, like this:

/**
 * Define JSON
 * @typedef {string} JSON
 */

and then, later on, use the JSON type in my function definitions etc.

See the line starting with @param below:

/**
 * Compresses JSON data
 * @function pressJSON
 * @param {JSON} myJSON - any valid JSON
 * @returns {string} a pressed version of the JSON input
 */

Further Reading:

  • https://jsdoc.app/tags-typedef.html
发布评论

评论列表(0)

  1. 暂无评论