重复使用打字稿接口之间的JSDOC评论

发布于 2025-01-27 22:06:02 字数 319 浏览 1 评论 0原文

假设我有以下接口:

interface Foo {
  /**
   * A date string in the format `yyyy-MM-dd`
   */
  archiveDate: string;
}

interface Bar {
  /**
   * A date string in the format `yyyy-MM-dd`
   */
  publishDate: string;
}

如您所见,两个接口之间的JSDOC说明完全相同。是否有一种方法可以共享两个(可能还有更多)接口之间的描述,因此我不必多次复制/粘贴相同的描述。

Let's say I have the following interfaces:

interface Foo {
  /**
   * A date string in the format `yyyy-MM-dd`
   */
  archiveDate: string;
}

interface Bar {
  /**
   * A date string in the format `yyyy-MM-dd`
   */
  publishDate: string;
}

As you can see, the JSDoc descriptions are exactly the same between the two interfaces. Is there a way of sharing the descriptions between the two (and possibly many more) interfaces so I don't have to copy/paste the same description many times.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小清晰的声音 2025-02-03 22:06:02

可以说,JSDOC评论是指此处的类型字符串,而不是属性。因此,您可以定义类型的别名并将DOC评论放在那里。

/**
 * A date string in the format `yyyy-MM-dd`
 */
type DateString = string;

interface Foo {
  archiveDate: DateString;
}

interface Bar {
  publishDate: DateString;
}

您甚至可以将格式编码为字符串字符类型:(

type Digit = '0' | '1' | '2' | '3' // | ... ;
type DateString = `${Digit}${Digit}${Digit}${Digit}-${Digit}${Digit}-${Digit}${Digit}`;

解决错误表达式产生的联合类型太复杂而无法表示。(2590)添加剩余数字作为练习reader)

Then you even get errors when you don't use the format:

const foo: Foo = {
  archiveDate: '2023-1-1'
}
Type '"2023-1-1"' is not assignable to type '"0000-00-00" | "0000-00-01" | "0000-00-02" | "0000-00-03" | "0000-00-10" | "0000-00-11" | "0000-00-12" | "0000-00-13" | "0000-00-20" | "0000-00-21" | "0000-00-22" | "0000-00-23" | ... 65523 more ... | "3333-33-33"'. Did you mean '"2023-01-01"'?(2820)

Playground

Arguably the JSDoc comment refers to the type string here, not the property. So you can define a type alias and put the doc comment there.

/**
 * A date string in the format `yyyy-MM-dd`
 */
type DateString = string;

interface Foo {
  archiveDate: DateString;
}

interface Bar {
  publishDate: DateString;
}

You can even encode the format as a string literal type:

type Digit = '0' | '1' | '2' | '3' // | ... ;
type DateString = `${Digit}${Digit}${Digit}${Digit}-${Digit}${Digit}-${Digit}${Digit}`;

(resolving the error Expression produces a union type that is too complex to represent.(2590) when adding the remaining digits is left as an exercise for the reader)

Then you even get errors when you don't use the format:

const foo: Foo = {
  archiveDate: '2023-1-1'
}
Type '"2023-1-1"' is not assignable to type '"0000-00-00" | "0000-00-01" | "0000-00-02" | "0000-00-03" | "0000-00-10" | "0000-00-11" | "0000-00-12" | "0000-00-13" | "0000-00-20" | "0000-00-21" | "0000-00-22" | "0000-00-23" | ... 65523 more ... | "3333-33-33"'. Did you mean '"2023-01-01"'?(2820)

Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文