接口属性的 TypeScript jsdoc

Mik*_*owe 16 jsdoc typescript visual-studio-code

我有一个带有单字符属性名称的 TypeScript 接口(设计约束)。我想使用 JSDoc 来记录这个界面以帮助在 vscode 中自动完成。

现在的界面如下:

export interface ISource {
    b: string
    d: string
    f: string
    h: string
    M: number
    L: number
    P: number
    n: string
    r: string
    u: string
    p: string
}
Run Code Online (Sandbox Code Playgroud)

非工作尝试是:

/**
* @typedef {object} ISource
* @property {string} ISource.b - Bias: bias_text[rating.bias[0]],
* @property {string} ISource.d - Domain: rating.domain.replace(/^www\./, ""),
* @property {string} ISource.f - FacebookUrl: _.lowerCase(rating.facebook_url),
* @property {string} ISource.h - Host: `https://${rating.domain}`,
* @property {number} ISource.M - MozRankUrl: rating.moz_rank_url,
* @property {number} ISource.L - MozLinks: rating.moz_links,
* @property {number} ISource.P - MozPopularity: rating.moz_popularity,
* @property {string} ISource.n - Source: rating.source,
* @property {string} ISource.r - Reporting: _.upperCase(_.kebabCase(_.first(rating.factual_reporting))),
* @property {string} ISource.u - Url: url,
* @property {string} ISource.p - Path: path,
*/

export interface ISource {
    b: string
    d: string
    f: string
    h: string
    M: number
    L: number
    P: number
    n: string
    r: string
    u: string
    p: string
}
Run Code Online (Sandbox Code Playgroud)

export interface ISource {
    b: string /** @property {string} b - Bias: bias_text[rating.bias[0]], */;
    d: string /** @property {string} d - Domain: rating.domain.replace(/^www\./, ""), */;
    f: string /** @property {string} f - FacebookUrl: _.lowerCase(rating.facebook_url), */;
    h: string /** @property {string} h - Host: `https://${rating.domain}`, */;
    M: number /** @property {string} M - MozRankUrl: rating.moz_rank_url, */;
    L: number /** @property {string} L - MozLinks: rating.moz_links, */;
    P: number /** @property {string} P - MozPopularity: rating.moz_popularity, */;
    n: string /** @property {string} n - Source: rating.source, */;
    r: string /** @property {string} r - Reporting: _.upperCase(_.kebabCase(_.first(rating.factual_reporting))), */;
    u: string /** @property {string} u - Url: url, */;
    p: string /** @property {string} p - Path: path, */;
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*ner 28

只需将文档注释放在每个属性之前:

export interface ISource {
    /**
     * Bias: bias_text[rating.bias[0]],
     */
    b: string

    /**
     * Domain: `rating.domain.replace(/^www\./, "")`
     */
    d: string
    ...
}
Run Code Online (Sandbox Code Playgroud)

(另外,不要在 TS 文件中的 JSDocs 中使用类型注释;编译器和工具将忽略这些类型)

  • 您可以将注释格式化为单行:`/** Docs here */ b: string` (7认同)
  • 有没有替代方案,例如 function @param. 这使得属性定义非常长。 (4认同)