为 jsdoc 编写类声明

发布于 2025-01-14 14:33:57 字数 2011 浏览 2 评论 0原文

我编写了一个简单的类和它的声明。然后我使用jsdoc指定lib的类型。

// index.ts
class VjsccXX {
  el: HTMLElement
  text: string
  show: boolean

  constructor(el: HTMLElement, text: string, show = true) {
    this.el = el
    this.text = text
    this.show = show

    this.el.classList.add('vjsscc-xx')

    if (!show) {
      this.el.style.display = 'none'
    }

    console.log('xx is ready')
  }
  hide() {
    this.el.style.display = 'none'
  }
  tex() {
    this.el.innerHTML = this.text
  }
}

export default VjsccXX
// index.d.ts
declare class VjsccXX {
  el: HTMLElement
  text: string
  show: boolean
  hide(): void
  tex(): void
  constructor(el: HTMLElement, text: string, show?: boolean)
}

export = VjsccXX
// test.js
/** @type {import('..')} */
const VjsccXX = window.VjsccXX

输入图片description here

但如上图所示,VjsccXX 变成了一个实例而不是一个类。那么该如何处理呢?


======================= 一些更新 ========================= =

上面显示的图像使用 window.VjsccXX 因为它的 html 文件包含一个 umd 包。它的类型不正确,因为 VjsccXX 应该是类构造函数而不是具有原型属性的类实例。

然后我编写另一个 ts 文件来查看其类型:

在此处输入图像描述

也许我可以这样写?

======================= 另一个更新========================= ===

也可与 cjs 配合使用:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

也许是jsdoc的问题?

I write a simple class and a declaration for it. Then I use jsdoc to specify the type of lib.

// index.ts
class VjsccXX {
  el: HTMLElement
  text: string
  show: boolean

  constructor(el: HTMLElement, text: string, show = true) {
    this.el = el
    this.text = text
    this.show = show

    this.el.classList.add('vjsscc-xx')

    if (!show) {
      this.el.style.display = 'none'
    }

    console.log('xx is ready')
  }
  hide() {
    this.el.style.display = 'none'
  }
  tex() {
    this.el.innerHTML = this.text
  }
}

export default VjsccXX
// index.d.ts
declare class VjsccXX {
  el: HTMLElement
  text: string
  show: boolean
  hide(): void
  tex(): void
  constructor(el: HTMLElement, text: string, show?: boolean)
}

export = VjsccXX
// test.js
/** @type {import('..')} */
const VjsccXX = window.VjsccXX

enter image description here

But as the photo shown above, VjsccXX become a instance rather than a class. So how to deal with it?

======================= Some Update ==========================

The image shown above use a window.VjsccXX because its html file include a umd bundle. And its type is not right because VjsccXX should be a class constructor rather than a class instance which has prototype properties.

Then I write another ts file to see its type:

enter image description here

Maybe I could write like that?

======================= Another update ============================

Also work with cjs:

enter image description here

enter image description here

enter image description here

Maybe it is the problem of jsdoc?

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

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

发布评论

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

评论(2

ヅ她的身影、若隐若现 2025-01-21 14:33:57

我认为你需要的是在这里使用 typeof

// test.js
/** @type {typeof import('..')} */
const VjsccXX = window.VjsccXX

I think what you need is to use typeof here:

// test.js
/** @type {typeof import('..')} */
const VjsccXX = window.VjsccXX
饮惑 2025-01-21 14:33:57

index.d.ts 中创建接口而不是类声明怎么样?

// index.d.ts
export interface IVjsccXX {
  el: HTMLElement;
  text: string;
  show: boolean;
  hide(): void;
  tex(): void;
}

然后你可以在jsdocs中使用它:

// test.js
/** @type {import('..').IVjsccXX} */
const VjsccXX = window.VjsccXX;

但是,TS中接口的构造函数处理对我来说是一团糟,如果有兴趣你可以参考具有构造签名的接口如何工作?了解详细信息。

What about creating an interface instead of a class declaration in index.d.ts?

// index.d.ts
export interface IVjsccXX {
  el: HTMLElement;
  text: string;
  show: boolean;
  hide(): void;
  tex(): void;
}

Then you may use it in jsdocs:

// test.js
/** @type {import('..').IVjsccXX} */
const VjsccXX = window.VjsccXX;

However, the constructor handling for interfaces in TS is a mess to me, if interested you may refer to How does interfaces with construct signatures work? for details.

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