为 jsdoc 编写类声明
我编写了一个简单的类和它的声明。然后我使用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
但如上图所示,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
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:
Maybe I could write like that?
======================= Another update ============================
Also work with cjs
:
Maybe it is the problem of jsdoc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你需要的是在这里使用
typeof
:I think what you need is to use
typeof
here:在
index.d.ts
中创建接口而不是类声明怎么样?然后你可以在jsdocs中使用它:
但是,TS中接口的
构造函数
处理对我来说是一团糟,如果有兴趣你可以参考具有构造签名的接口如何工作?了解详细信息。What about creating an interface instead of a class declaration in
index.d.ts
?Then you may use it in jsdocs:
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.