使用 JSDoc 在 ES6 类上设置索引签名
在 Typescript 中,您只需这样做:
class Test {
[key: string]: whatever
}
这允许您像这样访问计算属性名称...
class Test {
getProp(key) {
return this[key]
}
}
...而无需接收 Element 隐式具有“any”类型,因为类型“Test”没有索引签名。< /code>
我不知道如何使用 JSDoc 来完成相同的任务。有人有这样的运气吗?
In Typescript, you'd just do this:
class Test {
[key: string]: whatever
}
Which allows you to access computed property names like so...
class Test {
getProp(key) {
return this[key]
}
}
... without receiving Element implicitly has an 'any' type because type 'Test' has no index signature.
I can't figure out how to accomplish the equivalent with JSDoc. Anyone had any luck with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于单个方法,可以使用
@this
标记来执行此操作:尚不支持对整个类执行此操作。目前存在 TS 4.7 中的开放功能请求 (TypeScript#48096) .1 里程碑。然而,鉴于
Needs Proposal
标签,有人需要提出一个提议的 JSDoc 标签 + 语法,否则它可能最终会被推迟到该里程碑。For a single method, it's possible to do this with the
@this
tag:Doing it for the entire class isn't yet supported. There is an open feature request (TypeScript#48096) which is currently in the TS 4.7.1 milestone. However, given the
Needs Proposal
label, somebody needs to come up with a proposed JSDoc tag + syntax, or it will likely end up being pushed off that milestone.