简体   繁体   English

如何为类型化的 object function 参数的字段执行 Typescript JSDoc?

[英]How to do Typescript JSDoc for the fields of a typed object function parameter?

Below is a Typescript function signature x and it accepts ABC as optional parameter.下面是一个 Typescript function 签名 x,它接受 ABC 作为可选参数。 If pass in ABC, a and b are the required fields.如果传入ABC,a和b为必填字段。

async x (options: ABC = {}): Promise<string> 

interface ABC {
  a: string
  b: number
}

Should the JSDOC look like this JSDOC 应该是这样的吗

 /** 
   * @param {ABC} [options]
   * @param {string} options.a
   * @param {number} options.b
   * @return {Promise<string>} 
   */

Or this或这个

 /** 
   * @param {ABC} [options]
   * @param {string} [options.a]
   * @param {number} [options.b]
   * @return {Promise<string>} 
   */

Document the structure of ABC in the JSDoc for ABC not in the JSDoc for x :ABC的 JSDoc 中记录ABC的结构,而不是在x的 JSDoc 中:

interface ABC {
    /**
     * describe `a` here
     */
    a: string

    /**
     * describe `b` here
     */
    b: number
}

/**
 * @param {ABC} options
 * @return {Promise<string>}
 */
async function x (options: ABC): Promise<string> {
    // IOU a promise
}

If there is nothing to describe about ABC.a or ABC.b , the you can skip that JSDoc altogether.如果关于ABC.aABC.b没有什么可描述的,您可以完全跳过该 JSDoc。 Typescript and your Typescript IDE already know that a is a string and b is a number from the interface definition. Typescript 和你的 Typescript IDE 已经从接口定义中知道a是一个字符串, b是一个数字。 Your proposed JSDoc would be redundant even if it were correct (it is not).您提议的 JSDoc 将是多余的,即使它是正确的(它不是)。

The signature for x in the question produces an error, so I had to guess you intent.问题中x的签名会产生错误,所以我不得不猜测你的意图。 See my comment under the question.请参阅我在问题下的评论。

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

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